views:

55

answers:

2

I'm having some issues trying to setup my Routing in MVC. I think I understand how it works, but I just can't seem to set the proper paths.

Basically I want to do something similar to how StackOverflow works so:

http://localhost/faq

I want this to grab the HomeController, hit the faq action and return the faq view. I can't seem to figure out how to do this.

Also, I tried adding a new route for something like this:

http://localhost/Boxes/25

So, Boxes is the controller, 25 is obviously the id(parameter). Similar to how stackoverflow has: http://stackoverflow.com/questions/%5Bquestion number]/[question title]

So I tried doing this:

    routes.MapRoute(
         "Boxes",
         "Boxes/{boxnumber}",
         new {
             action="Details",  cubenumber = ""

} );

with no success.

I've also downloading the Route Tester app, but that doesn't seem to be helping at this point. Most likely I need to really read up on how routing works, but was just wondering if someone could point me in the right direction right now instead of me having to spin my wheels.

Thanks a lot guys!

+1  A: 

When you define a route, it has to at minimum contain two pieces of information: a controller and an action. Those values can either come through as a parameter (i.e. a "{parameter}" portion in the URL pattern), or as a default value.

The route example that you pasted above includes an action but it doesn't contain a controller, so it isn't capable of satisfying a request. Since your controller name is "BoxesController", you could simply add "controller='Boxes'" to the default values of that route and you'd be good.

To achieve the faq route, you could simply define a route whose URL was "faq" and had the default values: controller="Home", action="Faq".

LostInTangent
+2  A: 

Try the following:

routes.MapRoute(
         null, // optional route name
         "faq",
         new { controller="Home", action="Faq" } );

routes.MapRoute(
         null, // optional route name
         "Boxes/{boxnumber}",
         new { controller="Boxes", action="Details", boxnumber = ""} );

// Original route, if needed, should come AFTER more specialized routes.
routes.MapRoute(
         "Default",                                                // Route name
         "{controller}/{action}/{id}",                             // URL with parameters
         new { controller = "Home", action = "Index", id = "" } ); // Parameter defaults

Some notes that may help you understand this better:

  • the controller and action parameters must be specified, either explicitly in the incoming URL or via defaults that you specify (if missing in incoming URL)
  • the order of adding routes is significant because the first match will be used for each incoming URL. In the above example, if the original route is added first, the other ones will never be matched (because the original route specifies defaults for all parameterized parts of the URL)
  • route name is optional, only needed if you are using route names to generate outbound URLs
DSO
The second route (for Boxes) would hide all the actions in the Boxes method. Make sure you have a constraint on routes like these
Khaja Minhajuddin
How do you add a constraint?
Jack Marchetti