Victor Hugo Terceros
Victor Hugo Terceros

Reputation: 3169

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?

Upvotes: 8

Views: 5659

Answers (2)

Flavio Spedaletti
Flavio Spedaletti

Reputation: 398

You can use ActionName attribute to override the method name in API URL, and Route attribute to override the whole API URL.

Upvotes: 0

abhijeet abanave
abhijeet abanave

Reputation: 133

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

Upvotes: 5

Related Questions