MVC 3 Throughput Routing

Sorry to ask a pretty n00b question, but I'm pretty new to ASP.NET MVC. My problem is this:

I want my site to handle the url like this:

www.mysite.com/homepage/name

      

I want the above link to go to this user profile. For simplicity, the controller will be the controller of the main page with the test action.

We believe that the global.asax routing will be:

routes.MapRoute(
            "test",
            "homepage/{name}",
            new { controller = "Homepage", action = "Test" }
            );

      

So far, the code works fine (I tested it and it works fine).

But now my other features that I want include:

www.mysite.com/homepage/action/id

      

To work.

The routing for this would be:

routes.MapRoute(
            "Default",
            "homepage/{action}/{id}",
            new { controller = "Homepage", action = "Index", id = UrlParameter.Optional }
        );

      

The problem is, what happens when the user wants to omit {id} for an action, the routing table detects that the name of the action is actually the {name} parameter.

Is there a way to CHECK first if the action exists. And only if it is not, use it as a parameter for another route.

Has the meaning? if not, I'll add more details.

Thank!

EDIT

So, I managed to solve this using a constraint regex I put a regex defining all actions in my controller:

routes.MapRoute(
           "homepage",
           "homepage/{action}/{id}",
           new { controller = "Homepage", action = "Index", id = UrlParameter.Optional },
           new { action = "(action1|action2|action3)" }
           );

      

And then the next rule:

routes.MapRoute(
            "feed",
            "homepage/{id}",
            new { controller = "Homepage", action = "Test"}
            );

      

I work fine, only it is difficult to scale to it. You need to remember all new actions on the controller inside this line. This is a huge discovery for nightmares.

Thank!

+3


source to share


2 answers


First you need to put the route Default

and also restrict its allowed values

routes.MapRoute(
            "Default",
            "homepage/{action}/{id}",
            new { controller = "Homepage", action = "Index", id = UrlParameter.Optional },
            new {action = @"Index|Delete|Add"}
        );

routes.MapRoute(
            "test",
            "homepage/{name}",
            new { controller = "Homepage", action = "Test" }
            );

      



Thus, the first route will only be used if the action value matches the value specified in the constraint, otherwise it will proceed to the next rule.

You also need to make sure that when creating user profiles, the name does not have to match one of your supported route actions Default

.

0


source


Since ASP.NET MVC picks the first route from RouteTable

that matches the current request, you should fix this problem by reordering the routes in Global.asax.

First this:

routes.MapRoute(
            "Default",
            "homepage/{action}/{id}",
            new { controller = "Homepage", action = "Index", id = UrlParameter.Optional }
        );

      



and then this:

routes.MapRoute(
            "test",
            "homepage/{name}",
            new { controller = "Homepage", action = "Test" }
            );

      

Hope it helps.

+1


source







All Articles