I would like to receive a string as the id in the URL. Here is an example:
http://www.example.com/Home/Portal/Fishing
I would like to have Fishing in my id. But I cannot achieve it with the following code:
Code from my Controller:
public ActionResult Portal(string name)
{
// some code
ViewData["Portal Name"] = name;
}
Code from Global.asax.cs:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Just change the argument to id
:
public ActionResult Portal(string id)
{
// some code
ViewData["Portal Name"] = id;
}
The argument will be bound if it has the same name as the route value token. So an alternate approach would be to keep the argument named name
and change the route:
public ActionResult Portal(string name)
{
// some code
ViewData["Portal Name"] = name;
}
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
I would choose using id
, though, as it's the more standard approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With