MVC 3 How to Use MapRoute

Can someone show me how to use the MapRoute method? I tried to create my own routes, but it does not work. What I want to accomplish is a route that routes "http: //servername/home/default.aspx" to the "Home" controller and the "Default" action. Also, can it be said that if a user views the default.aspx file, does he actually point to the Index action?

I tried reading MSDN and googling links, but that did not make me wiser.

+4
source share
3 answers

The route you want to configure in the first part of your question:

routes.MapRoute( "", "home/default.aspx", new { controller = "Home", action = "Default" } ); 

Assuming you want to โ€œlook throughโ€ default.aspx with some kind of parameter, you can do something like:

 routes.MapRoute( "", "home/default.aspx/{param}", new { controller = "Home", action = "Default", param = UrlParameter.Optional } ); 

And then you will need to create your default action in order to accept the string parameter.

+4
source

It may be too late to help the developer who raised the question, but may help someone else. New to MVC, but what I found is apparently the map route, which is processed in the order in which they are added. I had a similar problem, my specific route did not work until I started adding the default route as the last route.

If the default routing route is added to your custom route and your custom URL matches the structure defined by the default map route, you will never reach your custom route.

+7
source

You must also ensure that the parameter name matches the name of the action parameter. Example:

  routes.MapRoute( name: "MyName", url: "{controller}/{action}/{myParam}", defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

MyController:

 public ActionResult MyAction(string myParam = "") { } 
0
source

All Articles