Saturday 24 October 2015

How to define route in Asp.Net Mvc?

How to define route in Asp.Net Mvc? public static void RegisterRoutes(RouteCollection routes)  {           routes.MapRoute(  ... thumbnail 1 summary

How to define route in Asp.Net Mvc?

public static void RegisterRoutes(RouteCollection routes)
 {
          routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // Route Pattern
        new
        {
                 controller = "Home",
                 action = "Index",
                 id = UrlParameter.Optional
         }// Default values for above defined parameters
      );
}
protected void Application_Start()
{
          RegisterRoutes(RouteTable.Routes);
 }

In above example we have defined the Route Pattern {controller}/{action}/{id} and also provide the default values for controller, action and id parameters.
Default values means if you will not provide the values for controller or action or id defined in the pattern then these values will be serve by the routing system.

                        www.xyz.com/Controller/Action/Id

For Example :-

         www.xyz.com
                    Controller name :-Home
                    Action :- index
                    Id :- none // default value for controller and action

        www.xyz.com/User
                   Controller name :-User
                   Action :- index
                   Id :- none

       www.xyz.com/User/ListOfUser
                  Controller name :-User
                  Action :- ListOfUser
                  Id :- none

       www.xyz.com/User/ListOfUser/1
                 Controller name :-User
                 Action :- ListOfUser
                 Id :- 1



No comments

Post a Comment