How to use attribute routing in ASP.NET Core

The routing middleware in ASP.Net Core is adept at mapping incoming requests to the respective

The routing middleware in ASP.Net Core is adept at mapping incoming requests to the respective route handlers. You can set up routing in ASP.Net Core in two various techniques: attribute-based routing and convention-based routing.

Contrary to convention-based routing, in which the routing information is specified at a one locale, attribute routing allows you to implement routing by decorating your motion procedures with characteristics. This report provides a dialogue of how we can perform with attribute-based routing in ASP.Net Core MVC.

To perform with the code examples supplied in this report, you ought to have Visual Studio 2019 mounted in your method. If you really don’t already have a copy, you can obtain Visual Studio 2019 in this article. 

Build an ASP.Net Core three.one MVC venture in Visual Studio 2019

1st off, let us produce an ASP.Net Core venture in Visual Studio 2019. Assuming Visual Studio 2019 is mounted in your method, adhere to the ways outlined underneath to produce a new ASP.Net Core venture in Visual Studio.

  1. Start the Visual Studio IDE.
  2. Click on on “Create new venture.”
  3. In the “Create new project” window, pick “ASP.Net Core Net Application” from the list of templates displayed.
  4. Click on Up coming.
  5. In the “Configure your new project” window, specify the title and locale for the new venture.
  6. Optionally verify the “Place remedy and venture in the identical directory” verify box, dependent on your tastes.
  7. Click on Build.
  8. In the “Create a New ASP.Net Core Net Application” window demonstrated future, pick .Net Core as the runtime and ASP.Net Core three.one (or later on) from the fall-down list at the top rated.
  9. Find “Web Software (Product-View-Controller)” as the venture template to produce a new ASP.Net Core MVC application. 
  10. Ensure that the verify packing containers “Enable Docker Support” and “Configure for HTTPS” are unchecked as we will not be working with those people functions in this article.
  11. Ensure that Authentication is set to “No Authentication” as we will not be working with authentication possibly.
  12. Click on Build.

Adhering to these ways will produce a new ASP.Net Core MVC venture in Visual Studio 2019. We’ll use this venture in the sections underneath to illustrate how we can perform with attribute routing in ASP.Net Core three.one.

Build a controller class in ASP.Net Core MVC

Build a new controller named DefaultController and change the default resource code of the DefaultController with the subsequent code:

Copyright © 2020 IDG Communications, Inc.