Configuring ASP.Net MVC 4 Routing with Custom Segment Variables

I just started working on an application with two areas (base system master / details type system). I am exploring the benefits of nice routing features in MVC (4 specifically), and I'm "just not" I suppose. "

Currently, the only defined route is the base one:

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

this is normal, it works with the areas that we have identified, so I assume that it should know where the user is, and direct it to the appropriate controller based on contexts on the location / area in which the user is located. good..

Now I'm trying to configure a new route that can handle

 /someController/someAction/{statusName} 

and in particular something like:

 /OrderManager/List/New 

AND

 /OrderManager/List/Viewed 

where "New" is the status of "New" and has the signature "Action":

 public ActionResult List(string statusName) 

I assumed that I could just add a new route below the default identifying “statusName” instead of Id, but, of course, how mechanism H will know the difference between:

/ controller1 / action

/ controller2 / action2 / new

I tried to add a “static” route as

 routes.MapRoute("Default", "ControllerName/ControllerAction/{statusName}", new { statusName = UrlParameter.Optional } ); 

I thought that I can "hiJack" only one route and do something special with it, but in order to know what to use, the router stops at the first match ?? I guess this was the wrong way to solve this problem anyway.

so now I get to the idea of ​​getting to something like:

 /somecustomroutename/somesortValue 

ex. / OrderManagerList / viewNew

where these routes will be mostly aliases. I thought adding the following route would do the trick:

  routes.MapRoute("Default_List", "OrderManagerList/{statusName}", new {controller="OrderManager", action="List", statusName= UrlParameter.Optional } ); 

with the appropriate action on the OrderManager controller:

 public ActionResult List(string statusName) 

no matter what I try, the argument is null, or "the resource cannot be found"

I know that controllers must have a corresponding View file. But this is not a problem, the problem is in my attempt to understand routing.

SB my questions .. basically, what am I missing about routing in MVC (4)? even some good articles for a simpleton, how am I to understand?

my understanding; identify the route and display its “endpoint” .. however, I think I don’t understand the assumption that the machine is doing ..

In any case, let me know if further explanation / editing is required.

early.

+4
source share
1 answer

The basic principle of routes is that they are ranked from top to bottom, and routing systems use the first match, not the best match.

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

Given this principle, let's look at your situation. Initially, you only define the default route:

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

URL pattern: "{controller} / {action} / {id}". By itself, this will match any three-segment URL and will not match a URL that has less than three segments. However, the third input parameter is the default parameter, which determines the default values ​​for the first and second segments and indicates that the third segment is optional. The next consequence of this is matching the route URL with 0.1, 2, or 3 segments.

Now you want to add a route where the third segment of the URL maps to the statusName parameter, and can handle URLs such as:

 OrderManager/List/New OrderManager/List/Viewed 

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

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

Please note that since this route is more specific than the default route, you must place 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 both of them will correspond to URLs having three segments. Suppose you decide that the new route will accept everything that contains only letters in the third segment, leaving the default route to handle everything that contains numbers. You can do this using route restrictions. For example, you can 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 with only letters in the third segment places the third segment in a variable called “statusName”, while any URL with an integer in the third segment puts the third segment in the variable called “id”.

Routes can be complicated in any real-life applications, and it is very useful to write unit tests for your routes to ensure that you do not hurt the situation when adding or changing a route.

For good routing links, see Scott Sanderson's book or see MSDN documentation

+10
source

Source: https://habr.com/ru/post/949177/


All Articles