S.C.
S.C.

Reputation: 920

Web API 2 default routing scheme

This question suddenly pops up in my mind.

In Startup.cs I have:

HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);

When I have a method like this one:

[RoutePrefix("api/Order")]
public class OrderController : ApiController
{
    // don't use any attribute here
    public IHttpActionResult HelloWorld()
    {
        ...
        return Ok();
    }
}

  1. Is it possible to access HelloWorld()?
  2. Should a GET or POST or whatever action be sent?

Upvotes: 1

Views: 626

Answers (1)

Xavier Egea
Xavier Egea

Reputation: 4763

You can access to HttpWorld() using GET if you rename your method as: GetHelloWorld(). The same with POST renaming to PostHelloWorld().

But I prefer using [HttpGet], [HttpPost], ... attributes, even if my action methods has the "Get" or "Post" characters in the name, to avoid possible errors.

Updated

After making some tests, I realised that my comments about that is not possible to call the HelloWorld are not correct. Indeed it is possible to call your HelloWorld() method if you make a POST call to http://<YourProjectUrl>/order.

So, the default method will be POST and, as you haven't configured any Route for your action method (take in account that RoutePrefix is just a prefix, so needs a Route attribute to be considered), it will get your controller name without "Controller" (OrderController -> Order).

Upvotes: 1

Related Questions