Routing

Routing is about understanding URL request and direct them to the right place to be handled properly.

ASP.Net comes with default route setting, which is enough for the most cases.

If you have special SEO strategy for your web site, which you should, mastering routing can help you construct better URLs.  Or simply, URLs can be more easy to read and understand for all users, not necessary for just SEO.

You can also make your website for International users and giving them url base culture settings like, yourdomain/en-US/ for English, youdomain/ja-JP/ for Japanese, and pulling different resource files.

When working with routing, you ll be working with classes under System.Web.Routing name space, which you can refer to https://msdn.microsoft.com/en-us/library/system.web.routing(v=vs.110).aspx.

You can find main configuration for Routing under App_Start and in RouteConfig.cs

By default it looks like this;

The system get URL request from the user, then start from the top and find matching URL request then handle.  If you are to add multiple MapRoute (which is the way to tell the system how to handle URL), you want to make sure the order is correct, because the order matters.

Looking at this default file, you will notice,RouteCollection variable routes is using method MapRoute() to set how URL should be handled.

Please refer to https://msdn.microsoft.com/en-us/library/system.web.mvc.routecollectionextensions.maproute(v=vs.118).aspx for detail of MapRoute().

Let’s take deeper look at default file,

Routing is a collection of instruction, so the first thing to do is naming the route.  Here we call this  “Default”.  Next, we specify URL patterns that applies to this route.  So here anything that fits in controller slash action slash id, it will be handled.  The default value is set, controller is Home and action is Index, and id is optional.  If you this route is applicable (which it is in this case) and don’t tell what controller and action are, then it will fall into default value, that is, controller is Home and action is Index.

Let’s say you want your user to look up to Home/Top when they type in just domain name, you can simply change your default to

 

 

 

Leave a Reply