ASP.NET 3.5 URL Routing in MVC Framework

Introduction

This post speaks about basics of URL Routing and how URL Routing related to building a ASP.NET MVC Application. You can also use the URL Routing with the ASP.NET Web application if install the Visual Studio 2008 service pack1. This post specifically speaks about how URL Routing is used in ASP.NET MVC Application.

  • URL Routing is critical element in the ASP.NET MVC Application.

  • The reason for this is URL Routing determines incoming request to get mapped to the MVC Controller.

  1. Open the ASP.NET MVC Application which is already created and open the web.config file in the project.

     Untitled-1.gif

    Figure 1

    Notice that within the module section in the web.config file there is something called UrlRoutingModule. This HTTP Module Intercepts all the incoming requests in the ASP.NET MVC Application.

  2. This HTTP Module uses the RoutingTable and this Route Table is set up in the Global.asax file.
     Untitled-2.gif

    Figure 2

    RegisterRoutes Method called when MVC Application stars and it sets up a route table that just contains a route. We create the routes in the route table by calling the MapRoute method.

    • The first parameter for the method is route name.

    • Second parameter is URL which specifies the pattern to intercept request. 3 segments will be there in this pattern. first segment is Controller, Second one is Action and the Third one is id.

    • The third parameter is for to set up the defaults, If you don't pass the controller in the URL then it takes the Home as default and if you don't mention the action it takes the Index and id as empty string.

  3. You can add the new controller by right clicking the Controllers folder and select the add new item.

    Untitled-3.gif

    Figure 3

    Add the action method Getprice in the code behind file

    Untitled-3i.gif

  4. Run the application you will see the following output

    Untitled-4.gif

    Figure 4

    So you can easily add new controllers and the actions in the MVC Application.
    For example if you want to create a BLOG which contains the Archives which contains the date of the entry.

    The URL looks something like this /Archive/12-20-1980

    Certainly this sort of requirement won't work with the default routing. we can not create a method for each end every date.

    We can handle this issue by going to Global.asax file and adding a new route to handle this issue.

    Untitled-4i.gif


  5. Now We have to create controller with name Archive to handle the incoming request and write method Entry method which takes the date time as a parameter.
    Untitled-5.gif

  6. Run the application you will see the following output

    Untitled-6.gif

    Figure 5

    Notice that we have entered the date in the URL and Entry method in the ArchiveController takes as parameter and returned the above output.

Conclusion

This post explained the basics of the URL Routing in the ASP.NET MVC Application and How to create custom route to map the incoming browser request. 

Up Next
    Ebook Download
    View all
    Learn
    View all