Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Routing In MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 361
    Comment on it

    What is Routing

    The concept of pattern matching process used to monitor the requests and determines further step for each request is known as Routing. In other words , the mechanism of mapping request in our MVC application is Routing. MVC application is by default configured to use routing.

    To Configure Routing in Asp.net application we need to first enable it in Web.Config file. Below sections of web.config file are used to configure Routing in mvc application: system.web.httpModules section, system.web.httpHandlers section, system.webserver.modules section, and the system.webserver.handlers section. On deleting these sections routing will no longer work

    MVC application starts from Application_Start () event of global.asax file . This method register all routes in the route table using RouteCollection.MapRoute method.

    What is route table

    RouteTable is a class that stores all the route information used in your web application, by default Routetable contains one route i.e. "default". This route contains the Controller name, Action Method name and the parameter name id (optional). In the web browser address bar we can see URL as "/Home/Index/1 . This means Controller - HomeController, Action method - Index and Id parameter = 1.

    There is a collection of routes called the **RouteCollection** that contains all the registered routes in the application. RegisterRoutes method registers the routes in this collection. A Route defines a URL pattern and a handler to use if the request matching the pattern.

    Lets say if we have a Action index method defined with non nullable parameters, requesting URL Home would throw and exception as id parameter cannot be passed null.

    Public ActionResult Index(int Id)
    {
         return view();
    }
    

    To solve this we make the Id parameter as null-able.

     Public ActionResult Index(int Id)
        {
             return view();
        }
    

    Custom Routes

    The above mentioned example is the default route used by the MVC application. We can also create our own custom application. Lets consider the example below:

    routes.MapRoute(
    name: "College",
    url: "Student/{studentId}",
    defaults: new { controller = "Student", action = "Details"}
    );
    

    Here the route is named as "College", Controller that handles the request is "Student" and action method is "Details". The default value of action will be used which we have defined as Details here. But one important point, "always place the more specific routes before the more general routes" since the order in which we register the routes is respected by the routing engine. So if we place the default route before the route that we have registered above our action method will never get called. Avoiding this mistake can save us a lot of debugging effort later on.

    Adding Constraints in Routes

    We have seen in above example that adding a placeholder in the url pattern of the route defines the variable value. We can also define default value of the placeholder, so that if the user does not give any value it use the default value. As we define the default value of the of the placeholder , we can also define contraints to the placeholder. Lets say, in the above example we have StudentId as placeholder and action method expects studentId as integer. If user gives this as string or some special character or anything else it will throw an exception. So we add a constraint to this placeholder in order to limit this as integer.We can add constraints as :

    routes.MapRoute(
    name: "College",
    url: "Student/{studentId}", 
    defaults: new { controller = "Student", action = "Details"},
    constraints:new{id=@"\d+"} 
    
    ); 
    

    If the URL matches the route but placeholder does not satisfy the constraint defined in the route table , then routing engine continues searching for the matching route. Hence this adds validation in the URL which can throw exception otherwise.

    Note: By using routing in MVC application we have one another big advantage and that is using routing we can easily create search engine optimized URLs It is because when using routing we are not restricted to fixed URLs and can very easily re-factor the URLs so that they are easily discovered by the search engines. SEO doesn't include only URL optimization though URL optimization is part of SEO.

    MVC 5 supports a new type of routing called Attribute Routing. As the name suggests, Attribute Routing enables us to define routing on top of the controller action method. Attribute Routing I will explain in my next blog.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: