Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspNet MVC Web Api, ActionName or Route

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?

like image 399
Victor Hugo Terceros Avatar asked Mar 01 '18 19:03

Victor Hugo Terceros


People also ask

What is the difference between MVC routing and Web API routing?

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.

What is difference between route and RoutePrefix?

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.

What makes it different a Web API routing from MVC routing Mcq?

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.

What is the role of Aspnet Web API?

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.


1 Answers

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

like image 122
abhijeet abanave Avatar answered Sep 20 '22 14:09

abhijeet abanave