Loading

ASP.NET Web API

How to implement Routing Variations in ASP.NET Web API?. The Complete ASP.NET Web API Developer Course 2022 [Videos].

In this Video, I am going to discuss Routing Variations in ASP.NET WEB API with examples. Please watch our previous Video before proceeding to this Video where we discussed the basics of Routing in ASP.NET Web API. As part of this Video, we are going to discuss the following important pointers.

  1. Understanding HTTP Methods.
  2. Routing Variation in Web API by Action Name.
  3. Understanding the NonAction attribute in ASP.NET Web API.
Understanding HTTP Verbs in ASP.NET Web API.

In the last Video, we discussed using the default naming convention i.e. default route provided by ASP.NET Web API Framework. Instead of using the default naming convention for the HTTP methods, you can also explicitly specify the HTTP method for action by decorating the action method with the HttpGet, HttpPut, HttpPost, or HttpDelete attribute.

Example: HttpGet, HttpPut, HttpPost, or HttpDelete Attribute in ASP.NET MVC

Let us understand this with an example. Please consider the following Student controller.

Understanding HTTP Verbs in Web API

In the above example, the FindAllStudents and FindStudentById actions are mapped to the GET request, while the RemoveStudent action is mapped to DELETE Request and the AddStudent action is mapped to POST Request. If you want to allow multiple HTTP verbs on a single action method, or if you to allow HTTP methods other than the GET, PUT, POST, and DELETE, then you need to use the AcceptVerbs attribute, which takes a list of HTTP methods as shown in the below image.

Understanding AcceptVerbs in ASP.NET Web API

In the above example, the FindStudentById action method is mapped to both GET and HEAD HTTP Request.

Routing Variation in Web API by Action Name

With the default routing template, the ASP.NET Web API Framework uses the HTTP method to select the action. However, if you want you can change this default behavior. Suppose, you want to create your own route where the action name is included as part of the URI, Then you need to create a new route as shown in the below image.

Routing Variations in WEB API

In the above route template, the {action} parameter names the action method on the controller. With this style of routing, it is mandatory to use the HTTP verb attributes to specify the allowed HTTP methods. Let us understand this with an example. Please consider the following controller.

Routing Variations in WEB API

In the above example, a GET request for “api/Student/FindAllStudents” would map to the FindAllStudents action method. In ASP.NET Web API by using the ActionName attribute you can also override the action method name. In the below Student Controller class, we have two actions which map to “api/Student/Image“. One action method supports the GET request while the other one supports the  POST HTTP request.

ActionName Attribute in ASP.NET Web API Application

Understanding the NonAction Attribute in ASP.NET Web API:

If you want to prevent an action method from getting invoked as a response to an HTTP request, then you need to decorate that action method with the NonAction attribute. This tells the ASP.NET Web API Framework that the method is not an action, even if it would match the routing rules.

Understanding the NonAction Attribute in ASP.NET Web API

As of now, we have discussed a high-level overview of ASP.NET Web API Routing. 

See All

Comments (325 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Web API / Blog

What is ASP.NET Web API Application?

In this ASP.NET Web API Tutorials series, I covered all the features of ASP.NET Web API. You will learn from basic to advance level features of ASP.NET Web API. The term API stands for “Application Programming Interface” and ASP.NET Web API is a framework provided by Microsoft which makes it easy to build Web APIs, i.e. it is used to develop HTTP-based web services on the top of .NET Framework.
3-Feb-2022 /34 /325

ASP.NET Web API / Blog

How to creat ASP.NET Web API Application using Visual Studio?

In this article, I am going to discuss the step-by-step procedure for Creating ASP.NET Web API Application. Please read our previous article before proceeding to this article where we gave an overview of the ASP.NET Web API framework. As part of this article, we ate going to discuss the following pointers.
3-Feb-2022 /34 /325

ASP.NET Web API / Blog

How to add Swagger in Web API Application?

In this article, I am going to discuss how to add Swagger in Web API Application to document and test restful Web API services. Please read our previous article where we discussed How to Create an ASP.NET Web API Application step by step before proceeding to this article as we are going to work with the same example. As part of this article, we are going to discuss the following pointers.
3-Feb-2022 /34 /325