views:

63

answers:

2

Just getting started with ASP.NET MVC and it's great! But I don't quit understand setting up routes.

How do I route ~/About to ~/Home/About?

/Views/Home/About.aspx

I would like to be able to access it with /Home/About or just /About

+4  A: 

If you want to explicity setup a route for it, you can do something like this:

routes.MapRoute( 
            "AboutRoute", 
            "About", 
            new { controller = "Home", action = "About" }  // Parameter defaults 
    );

I think thats what you want to do? I.e. have /About handled by the home controller?

The default route (as below) handles /Home/About

    routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );
RM
that was easy ;-) thanks!. I should have asked how to set it up with wildcards. so /Home/ContactUs goes to ContactUs
Brian Boatright
@Brian - not sure I fully get what you mean, do you mean you want any route in home controller to be accessed by /{action} rather than /{controller}/{action}? I wouldn't recommend it, it would mean you couldnt have the default route as above ("{controller}/{action}/{id}"). Easier to just specify the specific routes like for /About above for the home controller actions you want.
RM
+3  A: 

In response to your comment on RM's answer - you don't actually need wildcards for that. Just do

routes.MapRoute(
    "AllToHomeController",
    "{action}/{id}",
    new { controller = "Home", action = "Index", id = "" });

Note, however, that you need to place this route at the very end of your route table (and you'll have to delete the default route), as this will catch every url that comes in.

You can use Phil Haack's Route Debugger to verify that your routes pick up urls as you expect.

Tomas Lycken
thanks! gotta love SO, two answers in under 10m.
Brian Boatright