DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > ASP.NET MVC 6 Routing Technique

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.

Pramod Gawande user avatar by
Pramod Gawande
·
Aug. 15, 17 · Web Dev Zone · Tutorial
Like (2)
Save
Tweet
6.11K Views

Join the DZone community and get the full member experience.

Join For Free

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

ASP.NET MVC ASP.NET

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Working With Geospatial Data in Redis
  • Key Metrics and Measurements to Track Project and Product Performance
  • Snake-Based REST API (Python, Mamba, Hydra, and Fast API)
  • A BGP Guide for the Non-Network Engineer

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo