Configure Routes in ASP.Net MVC

ASP.NET MVC routes are responsible for determining which controller method(controller action) to execute for a given URL. They consist of the following properties:
1.Unique name
A name may be used as a specific reference to a given route
2.URL pattern
A simple pattern syntax that parses matching URLs into meaningful segments
3.Defaults
An optional set of default values for the segments defined in the URL pattern
4.Constraints
A set of constraints to apply against the URL pattern to more narrowly define the
URLs that it matches

When the application starts up, ASP.NET MVC discovers all of the application’s controllers by searching through the available assemblies for classes that implement the System.Web.Mvc.IController interface (or derive from a class that implements this interface, such as System.Web.Mvc.Controller) and whose
class names end with the suffix Controller.

When the routing framework uses this list to figure out which controllers it has access to, it chops off the Controller suffix from all of the controller class names. So, whenever you need to refer to a controller, you do
so by its shortened name, e.g., EmployeeController is referred to as Employee, and Home Controller becomes Home.

The default ASP.NET MVC project templates adds the below generic route that uses the following
URL convention to break the URL for a given request into three named segments,
wrapped with brackets ({}): “controller”, “action”, and “id”:
{controller}/{action}/{id}

This route pattern is registered via a call to the MapRoute() extension method that runs
during application startup (located in App_Start/RouteConfig.cs):

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);

Ex.

 

Programming is easy…. 🙂

Advertisements