Overview:
If you want to know how to create Custom Method Names in ASP.NET Core Web API or how to use multiple same signatures GET methods in ASP.NET Core Web API, then article is for you. Here, you will learn about which of the default method names are allowed in ASP.NET Core Web API for GET and how can we create our Custom Method Names.
Introduction:
In this article, I am going to discuss how to create Custom Method Names in ASP.NET Core Web API applications. Here, we will discuss about below points.
- Understanding the Default Convention used by ASP.NET Core Web API.
- How to create Custom Method Names in ASP.NET Core Web API?
- How to create custom URL in ASP.NET Core Web API?
Understanding the Default
Convention used by ASP.NET Core Web API.
Let’s understand the default convention used by ASP.NET Core Web
API to map the HTTP verbs GET, PUT, POST, and DELETE to methods on a controller
with an example. By default, the HTTP verb GET is mapped to a method in a
controller that has the name Get().
In the following TestController, the method name is Get() so by default convention, this is mapped to the HTTP verb GET. Even if we rename it to GetStatus() or GetXYZ() or CustomName() it will still be mapped to the HTTP verb GET. The word Get is case-insensitive. It can be lowercase, uppercase, or a mix of both.
Default Method Name:
GET Custom Method Name:
Output in above both cases:
These methods are accessible by URL /test because in Route attribute the controller name is passed as a parameter.
Now let’s keep both get methods in “TestController”
If we will run this application and try to access URL /test, the GET request gets failed with ambiguity error message. It is because of WebAPI is not able to differentiate which GET methods needs to be called.
Now the
problem is how can we access these two methods.
First, let’s see the
default implementation of the Route class which is present at the top of
controller name as an attribute.
The default route specifies the URL route as domain Name Followed by controller name. In our example, it will be http://localhost:xxxxx/test where “test” is the controller name.
How to create Custom Method Names in ASP.NET Core Web API?
To implement Custom Method Names in ASP.NET Core Web API, let’s first change the default implementation of the Route class as shown below where we include the action name as part of the route.
Now make a request with the same URL /test it will give us the following error
So change the URL as we need to include the action name in the URL as we do the changes in the Route attribute.
/test/Get
/test/CustomName
That’s it. We have successfully implemented Custom Method Names in ASP.NET Core Web API. Now you will get the response as expected.
That’s it. We have successfully implemented Custom Method Names in ASP.NET Core Web API. Now you will get the response as expected. You can add as many as custom methods with same signature and it will work successfully.
How to create custom URL in ASP.NET Core Web API?
Currently
we are accessing the Web API methods with this URL format http://localhost:xxxxx/{ControllerName}/{ActionName}
If we want to add something more in URL before the controller name then this also possible. For this, we have to modify Route attribute like below. Instead of xyz you can put anything.
Now, the new URL format for accessing Web API methods will be like below:
http://localhost:xxxxx/xyz/{ControllerName}/{ActionName}
I would like to have your feedback. If you
have any question, comments or suggestions you can comment below or can email
me on my email id.
Thank you for reading this article and by
giving your time.
No comments:
Post a Comment