ASP.NET MVC URLs URLs

In ASP.NET MVC, can routes be defined that can determine which controller to use based on the data type of the URL part?

For example:

routes.MapRoute("Integer", "{myInteger}", new { controller = "Integer", action = "ProcessInteger", myInteger = "" }); routes.MapRoute("String", "{myString}", new { controller = "String", action = "ProcessString", myString = "" }); 

Essentially, I want the following URLs to be handled by different controllers, although they have the same number of parts:

MYDOMAIN / 123

MYDOMAIN / ABC

PS The above code does not work, but it indicates what I want to achieve.

+6
c # asp.net-mvc url-routing
source share
1 answer

Yes, if you use constraints :

So:

  routes.MapRoute( "Integers", "{myInteger}", new { controller = "Integer", action = "ProcessInteger"}, new { myInteger = @"\d+" } ); 

If you put this route above your string route (which does not contain a restriction for @"\d+" ), it will filter out any routes containing integers, and everything that does not have integers will be transmitted and your string route will pick it up.

The real trick is that Routes can filter what happens on the basis of Regular Expressions , and that you can determine what should be picked up.

+7
source share

All Articles