Quantcast
Home / SOFTWARE DEVELOPMENT / WEB DEVELOPMENT / 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.

Facebook Comments

About admin

Check Also

ASP.NET Interview Question Part-2

1) How does the cookies work in asp.net? When a cookie is created it will …

ASP.NET Interview Question Part-1

1) What’s a bubbled event? The event processing routine for each object in complex control …

ADO.NET interview Questions-Part 2

1.What are the advantages of ADO.NET? The advantages of ADO.NET are as follows:- It is …

Leave a Reply

Your email address will not be published. Required fields are marked *