Introduction

When you request any page into MVC Application, it will go through the Routing Architecture. Your Routing system will decide your URL pattern. The default routing algorithm is like {controller}/ {action}/ {id} patterns. But it can be possible to change that pattern using Custom Routes.

In this article, we will show how to Create Custom Routes, Debugging Routes and Router Constraints.

Using Default Route

You can configure Route into the Global.aspx's Application Start Event. When you are creating an MVC application, it will already define Default route. Default route can be configured using the following code:

This code used to add Custom routes and it will match the URL like, Archive/12-25-2010.

Route Constraints

When you create a Custom Route, you can also include route constraints. These constraints are used to restrict the requests to match routes. There are three basic types of constraints:

Regular Expression Constraints

HttpMethod Constraints

CatchAll Values

Regular Expression Constraints

You can use regular expression constraints to check the pattern of the Route and prevent invalid requests. Regular expression is used to check any pattern like Currency, Date, time, etc.

In the Above BlogArchive Example, Url likes Archive/12-25-2010 and Archive/mybirthday.

It will allow. Now we have to prevent parameter which is not a date. So it is possible using regular expression constraints. For that, add the following code into Global.aspx file Register Route method.

The above code matches the entry date must match pattern of two decimals followed by dash followed by two decimals followed by dash followed by four decimals.

HTTP Method Constraints

You can match route with any type of the HTTP operation like POST, GET, etc. you want to prevent user to access particular URL when GET operation occurs but not POST operation occurs. You can also used AcceptVerbs instead of HttpMethod constraints.

For example, BlogArchive example inserts Action only executes when HTTP GET Operation performs. For that, you have to add the following code into Global.asax file Register Route method.

Catch All Routes

Generally, your URL must match with number of segments, but you want to match your URL, regardless of the number of the segments for that, we have to create Catch-All Parameter. Now take a simple example of the Catch All Routes for that add one controller named CatchAllController and add the following code into the controller.

Now it will match the following URL like, CatchAll/index/1/2/3/4/5/6/7 and CatchAll/a/b/c/d/e/f/g/h/.

A catch-all parameter must appear as the last parameter. Think of a catch-all parameter as a parameter array.

Debugging Routes

After creating Custom Routes, you have to check whether your routes work perfectly with your given URL or not. For that, you have to debug your routes. To Debug Routes, you have to download one DLL from the http://code.haacked.com/mvc-1.0/RouteDebug-Binary.zip and add a reference into your MVC Application.

Now debug route you have to write the following line of the code into the Global.asax Application Start Event.

RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

And run your application. You will see the below screen in your browser:

You can see all the custom routes into All Routes Table and it will show that your browser URL matches with which Routes. If it will match any Routes, it will show true into the Match Current Request Routes, otherwise it will show false. You can check with another URL, for that you have to write URL into the Address Bar.

Now write URL like, CatchAll/1/2/3/4/5/6/. It will match with the Catchall CustomRoutes.

In this way, you can check with other Routes too. It is possible that your URL matches with more than one Route, at that time the first match Routes Executes.

Conclusion

The goal of this article is to demonstrate the flexibility of the routing system provided by ASP.NET MVC. Hope this helps!

Share

About the Author

I have been working as a Software Engineer on Microsoft .NET Technology.I have developed several web/desktop application build on .NET technology .My point of interest is Web Development,Desktop Development,Ajax,Json,Jquey,XML etc.I have completed Master of Computer Application in May-2011.I'm not happy unless I'm learning something new.

Nice article, however in order to give you 5, I would have liked some information on how to rewrite the url, say for example if we have the code following code:
“../{action}/{id}”

and action is 9 then how to rewrite this to e.g. pants
and if id is 4 then write this to levis.
So the ending result should be something like and url stating domain.com/9/4 > should be rewritten to domain.com/pants/levis/