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 then the url pattren for you application will be{controller}/{action}/{id}. Hence you need to provide the controller name followed by action name and id if it is required.


Please enter your comment!
Please enter your name here