Having the Method/Action ObtainValue, I want to assign a different name to the method when it is called, so I use the ActionName attribute
[ActionName("GetValueByID")]
public string ObtainValue(int id)
{
return "value";
}
But I can also use the Route attribute, as shown below
[Route("Api/Values/GetValueByID")]
public string ObtainValue(int id)
{
return "value";
}
So my question is, is there a difference?, should use one or the other? what about if I use both, which one takes precedence?
If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.
The RoutePrefix attribute is used to specify the common route prefix at the controller level to eliminate the need to repeat the common route prefix on each and every controller action.
48) State differences between MVC and WebAPI MVC framework is used for developing applications which have User Interface. For that, views can be used for building a user interface. WebAPI is used for developing HTTP services. Other apps can also be called the WebAPI methods to fetch that data.
ASP.NET Web API is a framework that helps you to build services by making it easy to reach a wide range of clients including browsers, mobiles, tablets, etc. With the help of ASP.NET, you can use the same framework and same patterns for creating web pages and services both.
ActionName : is action (resource-specific) name to the method..Intention is to give user friendly name to the particular method eg. FetchEmployeeData to GetEmployee...you can't specify controller name for prefix with "actionname"
Route : is the define fully qualified URL (more generic) to the URL-Pattern ....it is use to understand full resource path to user...with "Route" you can specify controller name separated by "/"..same as when specify fully qualified URL route in maproute method in routeconfig
In simple words we can say that, "ActionName" is used for particular method(resource), and other side we use "Route" to define URL-Pattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With