I am trying to figure out how to route my application to a default controller/task/id when none is specified in a request.
Here is my one routing instruction...
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "LML",
action = "TaskLibrary",
id = 7
//id = UrlParame开发者_如何学Cter.Optional
} // Parameter defaults
);
Using this, if I enter in 'http://mywebsite/', the proper controller/action/id is called. However, I would like the URL to reflect this. Rather the URL remains untouched from what I entered.
Using routing, is there a way to affect the URL so that it redisplays synced with the controller/action/id it is showing by default? Or do I have to create some sort of redirect action?
Routing is about mapping a request to an action, not about redirecting.
You could change your default route parameters to default to another action that simply redirects to "LML/TaskLibrary/7"
I'm new to ASP.NET MVC 4, and often like to see where the change can be made. So, for those who'd like to see the previous answer in code...
In RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "LML", action = "TaskLibrary", id = 7 }
);
The main difference from the original example, is the addition of "defaults:".
I hope this helps someone!
精彩评论