Arcadian
Arcadian

Reputation: 4350

creating WebAPI with other method names

I know I have done this before, but when I create the default WebAPI from VS and tried to add a simple method like MySpecialMethod and then consume it from a client using httpClient, it errors with a 404 or id parameter is missing

I can call the Values/Get by doing something like this:

 GetAsync("api/Values").result

but it doesn't work if I do

 GetAsync("api/Values/MySpecialMethod").result

the two methods are identical for a simple test.

Upvotes: 1

Views: 41

Answers (2)

Adam Stewart
Adam Stewart

Reputation: 1993

Try using the RouteAttribute to help clarify your methods routes.

public class MyController : ApiController
{
    [Route("api/Values/MySpecialMethod")]
    public string MySpecialMethod()
    {

    }
}

If you want to use this method, be sure to update your RouteConfig in AppStart to contain the following

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();
}

Upvotes: 2

Pat
Pat

Reputation: 5282

Ensure your WebApiConfig is configured with the /api/{controller}/{action}/{id} entry. It sounds like you ony have /api/{controller}/{id}

Upvotes: 3

Related Questions