Showing posts with label Attribute Routing. Show all posts
Showing posts with label Attribute Routing. Show all posts

Saturday, August 17, 2019

Asp.Net Core Web API Attribute Routing with Parameters

We saw on how to define custom attribute routing for Asp.Net Core WebAPI action methods and how to access the action methods using the custom Route. In this post we shall move a step further and see on how to use custom routes to pass parameters to the action methods.

Let us create one more action method to the Department controller which accepts an id parameter in the route.

Attribute Routing in Asp.Net Core Web API

In the previous posts we saw on how to use the HTTP verbs GET, POST, PUT & DELETE to invoke different action methods in an Asp.Net Core WebAPI controller, this approach holds good as long as we have only one Action method per verb in a controller, what if we want multiple methods with the same header or if we want to have custom routing to each action method, this is where attribute routing can help us. In this post we shall see on how to define custom routes to action methods using attribute routing.

Let us another controller DepartmentController, and add a Get method with a custom routing attribute as follows.

Sunday, May 26, 2019

Attribute routing in Asp.Net Core MVC

In the previous post we saw about Routing in Asp.Net Core MVC Applications, and how to define Routing in the Applications Startup, this is called conventional routing. Asp.Net Core MVC applications also provide us another way to define routes called Attribute Routing.

Attribute Routing
As the name suggests Attribute Routing is defined by defining routes in action methods using a Route attribute as follows.