Setting up ASP.Net MVC 4 Routing with custom segment variables

后端 未结 1 2014
轮回少年
轮回少年 2021-02-10 17:15

I just began working on an application with a couple areas (basic grid master / details type system..) I\'m looking into taking advantage of the nice routing features in MVC (4

相关标签:
1条回答
  • 2021-02-10 17:41

    The basic principle of routes is that they are evaluated from the top down, and the routing systems uses the first match, not the best match.

    The implication of this is that you must order your routes in order of specificity, with the most specific route first and the most general route last.

    With this principle in mind, let’s look at your situation. Initially, you have only the default route defined:

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

    The URL pattern is "{controller}/{action}/{id}". By itself, this would match any three-segment URL, and would not match any URL that had less than three segments. However, the third input parameter is the default parameter, which defines defaults for the first and second segment and indicates that the third segment is optional. The next effect of this is to make the route match URL having 0,1, 2 or 3 segments.

    Now you want to add a route where the third URL segment is mapped to a “statusName” parameter, and can handle URLs like:

    OrderManager/List/New 
    OrderManager/List/Viewed 
    

    There are two basic approaches you can take here. You can 1) create a very specific route that will handle these two URLs only, or 2) you can try and create a more general route to handle the general case. Let’s look at the first case first. You can create a route as follows:

    routes.MapRoute("", "OrderManager/List/{statusName}",
                  new { Controller = "OrderManager", Action = "List" });
    

    Note that because this route is more specific than the default route, you must put this route before the default route.

    If you want to have a more general route, you need to decide how this route will differ from the default route, since they both will match URLs having three segments. Let’s say you decide that the new route will accept anything that only contains letters in the third segment, leaving the default route to handle anything containing numbers. You can do this using route constraints. For example you could write a route as follows:

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { id = @"^\d+$" });
    
            routes.MapRoute(
                name: "NewRoute",
                url: "{controller}/{action}/{statusName}",
                defaults: new { Controller = "OrderManager", Action = "List" },
                constraints: new { statusName = "^[A-Za-z]+$" });
    

    With these two routes, any three-segment URL having only letters in the third segment will put the third segment into a variable called “statusName”, whereas any URL with an integer in the third segment will put the third segment into a variable called “id”.

    In any applications of real complexity, routes can get complicated, and it is very advantageous to write unit tests for your routes, to insure that you don’t screw things up when you add or modify a route.

    For good references on routing, see Scott Sanderson's book or see the MSDN documentation

    0 讨论(0)
提交回复
热议问题