Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API route to action name

I need a controller to return JSON to be consumed by JavaScript so I inherited from the ApiController class but it isn't behaving as I expected. The Apress book Pro ASP.NET MVC 4 and most of the online examples I've found give examples like:

public class ServicesController : ApiController
{
    public string[] MethodFruit()
    {
        return new string[] { "Apple", "Orange", "Banana" };
}

accessed via the URL:

http://mysite/services/methodfruit

But that never works - the resource isn't found. The only approach I can get working is to have the controller contain a different method for each HTTP verb, then:

http://mysite/api/services

Which calls the GET method.

I checked the Apress website but they don't seem to have any forums and the current source code is in VS 2012 which I'm not using. I examined the source files and they seem to think the former approach should work. Is the former approach no longer supported?

like image 900
Dave Avatar asked Sep 06 '13 16:09

Dave


People also ask

What is action name in Web API?

To find the action, Web API looks at the HTTP verb, and then looks for an action whose name begins with that HTTP verb name. For example, with a GET request, Web API looks for an action prefixed with "Get", such as "GetContact" or "GetAllContacts".

How do I specify a route in Web API?

As the name implies, attribute routing uses [Route()] attribute to define routes. The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config. MapHttpAttributeRoutes() method.

How do I give alias for an action method in Web API?

An alias name for an action method is provided by using ActionName attribute. Also it is necessary to change the route template in the WebApiConfig. cs.

What is convention based routing in Web API?

What is Convention Based Routing in ASP.NET MVC? Routing is a mechanism which is used to handle the incoming requests coming from browsers and it represent the particular action rather than any static or physical files.


2 Answers

Yep... generally you have to follow the default naming convention expected by ASP.NET WEB API.

Check this official doc:

Routing in ASP.NET Web API

If you do not want to follow the convention, you can try the Routing by Action Name section described in the above linked doc.

Routing by Action Name

With the default routing template, Web API uses the HTTP method to select the action. However, you can also create a route where the action name is included in the URI:

routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional } );

In your case, you'd have to do this:

[HttpGet]
public string[] MethodFruit()
{
    return new string[] { "Apple", "Orange", "Banana" };
}
like image 171
Leniel Maccaferri Avatar answered Nov 10 '22 12:11

Leniel Maccaferri


If you want Web API to look for the action name when routing, change the WebApiConfig.cs class in the App_Start folder to this:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Then you can just make a GET request to

http://mysite/api/Services/MethodFruit
like image 36
Mahesh Avatar answered Nov 10 '22 11:11

Mahesh