简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM