Home / Dot Net / How to define a route in ASP.NET MVC?

How to define a route in ASP.NET MVC?

You can define a route in ASP.NET MVC as given below:

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); //TODO: }

Always remember route name should be unique across the entire application. Route name can’t be duplicate. 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. Suppose your webapplication is running on www.example.com then the url pattren for you application will be www.example.com/{controller}/{action}/{id}. Hence you need to provide the controller name followed by action name and id if it is required.

About admin

Check Also

Latest Book Marking List 2017

S.No. High PR Social Bookmarking Websites Lists Google PR Alexa MozRank Moz PA 2 www.google.com/bookmarks …