开发者

Generic ASP.NET MVC Route Conflict

I'm working on a Legacy ASP.NET system. I say legacy because there are NO tests around 90% of the system. I'm trying to fix the routes in this project and I'm running into a issue I wish to solve wi开发者_如何学JAVAth generic routes.

I have the following routes:

        routes.MapRoute(
             "DefaultWithPdn",
             "{controller}/{action}/{pdn}",
             new { controller = "", action = "Index", pdn = "" },
             null
         );

        routes.MapRoute(
           "DefaultWithClientId",
           "{controller}/{action}/{clientId}",
           new { controller = "", action = "index", clientid = "" },
           null
       );

The problem is that the first route is catching all of the traffic for what I need to be routed to the second route. The route is generic (no controller is defined in the constraint in either route definition) because multiple controllers throughout the entire app share this same premise (sometimes we need a "pdn" sometimes we need a "clientId").

How can I map these generic routes so that they go to the proper controller and action, yet not have one be too greedy? Or can I at all? Are these routes too generic (which is what I'm starting to believe is the case).

My only option at this point (AFAIK) is one of the following:

In the contraints, apply a regex to match the action values like: (foo|bar|biz|bang) and the same for the controller: (home|customer|products) for each controller. However, this has a problem in the fact that I may need to do this:

~/Foo/Home/123 // Should map to "DefaultwithPdn"

~/Foo/Home/abc // Should map to "DefaultWithClientId"

Which means that if the Foo Controller has an action that takes a pdn and another action that takes a clientId (which happens all the time in this app), the wrong route is chosen.

To hardcode these contstraints into each possible controller/action combo seems like a lot of duplication to me and I have the feeling I've been looking at the problem for too long so I need another pair of eyes to help out.

Can I have generic routes to handle this scenario? Or do I need to have custom routes for each controller with constraints applied to the actions on those routes?

Thanks


Add constraints to your routes by removing the null and replacing it with the constraint needed for that route:

For PDN, use a regular expression for digits:

   routes.MapRoute(
         "DefaultWithPdn",
         "{controller}/{action}/{pdn}",
         new { controller = "", action = "Index", pdn = "" },
         new { pdn = @"\d+" } 
     );

For ClientID, use a regular expression for all characters:

    routes.MapRoute(
       "DefaultWithClientId",
       "{controller}/{action}/{clientid}",
       new { controller = "", action = "index", clientid = "" },
       new { clientid = @"[A-Za-z]+" }
   );

Since I don't keep the minutia of regular expression stuff in my head, I generally use a cheat sheet.


You should add some route constraints that say the PDN route matches numbers and the ClientId matches strings

I usually create a series of matches to use throughout my route declaration like so:

readonly static string ALPHA_MATCH = @"[\da-zA-Z]";
readonly static string DIGIT_MATCH = @"\d+";

then add the constraints to the routes like this:

routes.MapRoute(
    "DefaultWithPdn",
    "{controller}/{action}/{pdn}",
    new { controller = "", action = "Index", pdn = "" },
    new { pdn = DIGIT_MATCH }
);

routes.MapRoute(
    "DefaultWithClientId",
    "{controller}/{action}/{clientId}",
    new { controller = "", action = "index", clientid = "" },
    new { clientId = ALPHA_MATCH }
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜