How to use attribute routing in ASP.NET Core

Jeffrey Cuebas

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

Compared with conference-primarily based routing, in which the routing facts is specified at a single place, attribute routing enables you to apply routing by decorating your action strategies with characteristics. This posting offers a dialogue of how we can get the job done with attribute-primarily based routing in ASP.Internet Core MVC.

To get the job done with the code illustrations presented in this posting, you need to have Visible Studio 2019 set up in your program. If you really do not already have a copy, you can obtain Visible Studio 2019 below. 

Generate an ASP.Internet Core 3.one MVC challenge in Visible Studio 2019

First off, let’s create an ASP.Internet Core challenge in Visible Studio 2019. Assuming Visible Studio 2019 is set up in your program, adhere to the actions outlined beneath to create a new ASP.Internet Core challenge in Visible Studio.

  1. Start the Visible Studio IDE.
  2. Simply click on “Create new challenge.”
  3. In the “Create new project” window, choose “ASP.Internet Core World wide web Application” from the listing of templates shown.
  4. Simply click Next.
  5. In the “Configure your new project” window, specify the identify and place for the new challenge.
  6. Optionally verify the “Place remedy and challenge in the very same directory” verify box, depending on your tastes.
  7. Simply click Generate.
  8. In the “Create a New ASP.Internet Core World wide web Application” window proven next, choose .Internet Core as the runtime and ASP.Internet Core 3.one (or later on) from the fall-down listing at the prime.
  9. Pick out “Web Application (Model-Look at-Controller)” as the challenge template to create a new ASP.Internet Core MVC application. 
  10. Be certain that the verify boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be utilizing people capabilities below.
  11. Be certain that Authentication is established to “No Authentication” as we won’t be utilizing authentication possibly.
  12. Simply click Generate.

Subsequent these actions will create a new ASP.Internet Core MVC challenge in Visible Studio 2019. We’ll use this challenge in the sections beneath to illustrate how we can get the job done with attribute routing in ASP.Internet Core 3.one.

Generate a controller course in ASP.Internet Core MVC

Generate a new controller named DefaultController and change the default source code of the DefaultController with the subsequent code:

Copyright © 2020 IDG Communications, Inc.

Next Post

The community hat rules the company hat in open source

Id issues in open resource, but not how you may possibly consider. For example, Lili Cosic functions for Purple Hat, and she’s also a maintainer inside of the Kubernetes group, liable for kube-state-metrics. Although Purple Hat encourages Cosic in her Kubernetes do the job, they don’t manage her involvement. Open […]