简体   繁体   中英

WebAPI Help page shows different names than controller names

I have a controller that looks like this

    [HttpGet]
    [ActionName("Ping")]
    public bool Ping() { return true; }

    [HttpPost]
    [ActionName("Test")]
    public string Test([FromBody] Testing form) 
    { 
       return form.Email + " " + form.Password; 
    }

and when running the Help page shows this:

GET api/Test
POST api/Test

The routing is the default one that comes with the Template so not sure why it's not picking the right names even after putting the Annotation "ActionName".

Any help will be truly appreciated.

=================================================================
ANSWER: As suggested by David L. the problem was the Routing. I read his suggested link and added a new routing API that contained "ACTION" on it and it looks like this:

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

With this it nows displays the proper APIs using the proper Annotation name.

ActionName is part of the System.Web.Mvc namespacing. It is most likely getting ignored by WebAPI. WebAPI is going to give preference to your Get and Post tags first and foremost.

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP method, not the URI path, to select the action. You can also use MVC-style routing in Web API. This article does not assume any knowledge of ASP.NET MVC.

You can find more information about WebAPI routing as well as the quote above, at: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

EDIT:

To highlight what the help pages are trying to tell you, api is the root of your exposed api. Test is the controller route. It will always try to identify at that point with a REST verb, in this case, GET and POST, which is what it displays.

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