• ASP.NET MVC Routing

    by  • December 22, 2015 • Microsoft ASP.NET MVC • 0 Comments

    This article illustrates ; what is MVC Routing ? MVC Routing is responsible for mapping incoming browser requests (Actions) to MVC Controller. Let me explain this with the help of following diagram.

    When anyone sends request through a website address like www.asifpizza.com/ContactUs or www.TheWorld.com/ AboutUs as shown in the figure 1.1; The Actions ContactUs or AboutUs are mapped to controller using Routing.

    The standard MVC project file contains the route mapping configuration in a file namedĀ RouteConfig.cs:

    Lets look at RoutConfig.cs file


    The above code shows, we can define a rout by MapRoute() function above. In map rout function, there is three important inputs.

    1. What is the name of the Route.
    2. Second Parameter is the URL, that how URL structured.
    3. Third parameter is mapping URL to Controller, means to which Controller this url is mapped.

    The route above establishes a default value for both the controller and the action, in case one more both are not provided as part of an incoming URL.

    We can take this a step further, and add a new, more specific route which, when it matches a specific URL pattern, calls into one specific controller. In the following, we have added a new route mapping in our RouteConfig.csĀ file:

    Lets look at the following Code.

    In the above code, i add a new customized route by adding another MapRoute() function. Now we have mapped our action to Fruits Controller and Details action. if the the default route doesnot mapped then this route will be mapped. It means order is important. At First the Default route will map.


    Best WordPress Themes and Plugins with Great Team and Support!

    Leave a Reply