How to use attribute routing in ASP.NET Core

Maria J. Danford

The routing middleware in ASP.Net Core is adept at mapping incoming requests to the respective route handlers. You can established up routing in ASP.Net Core in two different approaches: attribute-centered routing and conference-centered routing. Not like conference-centered routing, in which the routing facts is specified at a one spot, attribute […]

The routing middleware in ASP.Net Core is adept at mapping incoming requests to the respective route handlers. You can established up routing in ASP.Net Core in two different approaches: attribute-centered routing and conference-centered routing.

Not like conference-centered routing, in which the routing facts is specified at a one spot, attribute routing enables you to employ routing by decorating your motion solutions with characteristics. This article presents a discussion of how we can function with attribute-centered routing in ASP.Net Core MVC.

To function with the code examples presented in this article, you need to have Visual Studio 2019 set up in your method. If you don’t presently have a copy, you can down load Visual Studio 2019 here. 

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

1st off, let’s make an ASP.Net Core venture in Visual Studio 2019. Assuming Visual Studio 2019 is set up in your method, adhere to the techniques outlined beneath to make a new ASP.Net Core venture in Visual Studio.

  1. Start the Visual Studio IDE.
  2. Click on “Create new venture.”
  3. In the “Create new project” window, select “ASP.Net Core World wide web Application” from the checklist of templates exhibited.
  4. Click Up coming.
  5. In the “Configure your new project” window, specify the name and spot for the new venture.
  6. Optionally check out the “Place answer and venture in the identical directory” check out box, depending on your choices.
  7. Click Create.
  8. In the “Create a New ASP.Net Core World wide web Application” window proven future, select .Net Core as the runtime and ASP.Net Core three.one (or later on) from the drop-down checklist at the leading.
  9. Find “Web Software (Design-Look at-Controller)” as the venture template to make a new ASP.Net Core MVC software. 
  10. Make sure that the check out packing containers “Enable Docker Support” and “Configure for HTTPS” are unchecked as we will not be employing those people options here.
  11. Make sure that Authentication is established to “No Authentication” as we will not be employing authentication possibly.
  12. Click Create.

Following these techniques will make a new ASP.Net Core MVC venture in Visual Studio 2019. We’ll use this venture in the sections beneath to illustrate how we can function with attribute routing in ASP.Net Core three.one.

Create a controller class in ASP.Net Core MVC

Create a new controller named DefaultController and replace the default source code of the DefaultController with the pursuing code:

Copyright © 2020 IDG Communications, Inc.

Next Post

The community hat rules the company hat in open source

Identification matters in open resource, but not how you may well believe. For example, Lili Cosic is effective for Pink Hat, and she’s also a maintainer in just the Kubernetes group, dependable for kube-point out-metrics. Even though Pink Hat encourages Cosic in her Kubernetes do the job, they really don’t […]

Subscribe US Now