Routing in MVC3 Application

In this article I am sharing my thoughts on to create a routing system in a MVC application and the terminology being used.

Here we go

RouteData

This mechanism is used to examine an incoming URL and then decide which controller and action the request needs to do the send.

Open your Visual Studio, click on "File" -> "New" -> "Project..." and select ASP.NET MVC3 Web Application, as in the following:

Routing1.jpg

Specify whatever name of your first application that you desire and click "Ok".

A new window will appear, from that window I chose Intranet application where there are many options. Another interesting fact is that there is a dropdown named "View Engine"; I selected Razor (which is more specific to MVC 3 and MVC 4) and I kept the checkbox "Create a new unit test" unchecked.

Routing2.jpg

Click on the "Ok" button.

The following depicts what the default screen is that comes after clicking on the "OK" button having multiple folder's.

Routing3.jpg

Let's go into more detail for this. Routing is defined in the Global.asax; open the file, and you will see the following lines of code:

Routing4.jpg


We need to make some adjustements in this to understand how routing works. The Application_Start method is responsible for registering the RegisterRoutes method. This Application_Start method is called by the ASP.NET platform when the application is first started.

Let's see what happens when we run our application first time with the default routing settings.

Routing5.jpg

Also for the About page I hit this URL http://localhost:53098/Home/About

Routing6.jpg


Routing7.jpg

The job of the routing system is to verify the incoming URL request and determine the value of segment variables; segment variables are defined using curly braces {}.

Defaults Values

routes.MapRoute(

                "Default", // Route name

                "{controller}/{action}/{id}", // URL with parameters

                new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults

            );

Since the default routing is defined above, it means whenever we run the application it will first call to the Index action of the Home Controller. On the other side if we specify this URL "http://localhost:53098/Home/About" then it will route to the controller = Home and action = About as it is explicitly mentioned.

Static URL

We can also create a static URL, static URL implies where you want to prefix the URL with a word "Views" or with other specific names e.g. abc, employee etc.

Since I've used in my application:

routes.MapRoute(

              "RegisterUser", // Route name

              "Views/{controller}/{action}", // URL with parameters

              new { controller = "Register", action = "RegisterAction"// Parameter defaults

          );

This URL pattern will match only URLs that contain three segments, the first of which must be Views. The other two segments can contain any value, and will be used for the controller and action variables.

Here I declared id=4 as the default if we don't specify and id value in URL, it would take 4 as the default id.

 

routes.MapRoute (

              "RegisterUser", // Route name

              "Views/{controller}/{action}", // URL with parameters

              new {controller = "Register", action = "Details", id = "4"// Parameter defaults

          );



If we hit the given URL "http://localhost:53098/Views/Register/Details"then it hits the Details action method having id=4.

Routing8.jpg

MVC also gives us leverage to have optional URL segments which means a URL segment that the user does not need to specify, but for which even no default value is also specified. In more layman terms, such a routing URL pattern will always run whether you use a value for third parameter or not.

e.g.: http://localhost:53098/
http://localhost:53098/Home
http://localhost:53098/Home/index
http://localhost:53098/Home/index/4
 

routes.MapRoute(

                "Default", // Route name

                "{controller}/{action}/{id}", // URL with parameters

                new {controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults

            );

Enjoy MVC and Routing.

Kindly inform me if you have any query.

Up Next
    Ebook Download
    View all
    Learn
    View all