Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ASP.NET MVC 6 Routing Technique

DZone's Guide to

ASP.NET MVC 6 Routing Technique

In this tutorial, we explore two new routing techniques introduced to ASP.NET by MVC 6, and explain the benefits they have on your code.

· Web Dev Zone
Free Resource

Get deep insight into Node.js applications with real-time metrics, CPU profiling, and heap snapshots with N|Solid from NodeSource. Learn more.

Before we jump to custom routing, we will have to take a look at the basic changes in MVC6 from MVC5. ASP.NET MVC6 places all startup necessary services, defined and configured dependencies for the application, in a single file, Startup.cs. The Startup.cs file replaces all the functionality of global.asax where we put the middleware and configuration logic.

As we know, the routing actions are getting managed by the Routes.MapRoute method. In ASP.NET MVC 6 Routes.MapRoute, you won’t find this method as a part of Startup.cs file which now contains very few lines of code. You will also notice that there is no ad-hoc configuration file to take care of RouteConfig.cs, WebApiConfig.cs or other intermediate handlers recurrently shipped with the earlier ASP.NET versions and MVC pattern project templates. Startup.cs file has services.AddMvc() and services.UseMvc() methods to handle the routing action.

We can take a look at the current implementation of the UseMvc() method in the application code.

Image title

We can add our own routing using an Attribute Based and/or Convention Based approach. I am using both methods because if we use two approaches together Attribute based routing overrides Convention based routing. Both routings would override the existing default routes defined by the UseMvc() method.

Attribute Based Routing

We have to write code in the controller to implement attribute based routing.

Image title

Convention Based Routing

We have to write code in Startup.cs to implement attribute based routing.

Image title

Node.js application metrics sent directly to any statsd-compliant system. Get N|Solid

Topics:
mvc 6 ,asp.net mv6 ,web dev ,asp.net development

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}