6

Could someone show me how to use the MapRoute method? I have tried creating my own routes, but it's not working. What i want to accomplish is a route that routes "http://servername/home/default.aspx" into controller "Home" and action "Default". Also, would it be possible to say that if the user is browsing the default.aspx "file", it would actually point to the "Index" action?

I have tried reading the MSDN references and googling, but it didn't make me any wiser.

Anton Gildebrand
  • 3,641
  • 12
  • 50
  • 86
  • You need to explain more. Are you trying to show WebForms in MVC, or are you saying that you want to create a Route that routes to controller = "Home", action = "Index" when "http://servername/home/default.aspx" is entered into the browser? – bdparrish Jun 22 '11 at 12:35

3 Answers3

11

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

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

Darren Hughes
  • 111
  • 1
  • 2
7

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

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

Assuming you wish to 'browse' default.aspx with some sort of parameter you can do something like:

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

And you would then need to create your Default action to accept string param.

Antonio Haley
  • 4,702
  • 1
  • 27
  • 19
  • That's how i thought i would use the maproute method, but when i try to browse the site (home/default.aspx) i get error 404 – Anton Gildebrand Jun 22 '11 at 12:47
  • Hmm, try running that on a brand new ASP.NET MVC3 solution. That's what I did and it worked just fine. There's got to be something else preventing the route from being resolved. – Antonio Haley Jun 22 '11 at 12:53
  • I had to comment out the default MapRoute to make it work. Have no idea why, but now im able to add as many MapRoute's as desired and it chooses the right one to use without any problems. Question: How could i rewrite the keyword "Default" so it says "Index" instead. I.E. when i'm browsing home/default.aspx it points to home-controller and action Index instead of action Default? – Anton Gildebrand Jun 22 '11 at 13:48
2

You also have to make sure the parameter name is the same as the action's parameter name. 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 = "")
{

}
live-love
  • 48,840
  • 22
  • 240
  • 204