简体   繁体   English

WebAPI帮助页面显示的名称与控制器名称不同

[英]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 GET API /测试
POST api/Test POST api /测试

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". 路由是模板随附的默认路由,因此即使确定添加了“ ActionName”注释,也不确定为什么它没有选择正确的名称。

Any help will be truly appreciated. 任何帮助将不胜感激。

================================================================= ================================================== ===============
ANSWER: As suggested by David L. the problem was the Routing. 答案:正如David L.所建议的那样,问题在于路由。 I read his suggested link and added a new routing API that contained "ACTION" on it and it looks like this: 我阅读了他的建议链接,并添加了一个新的路由API,其中包含“ ACTION”,看起来像这样:

 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. 现在,它使用正确的注释名称显示正确的API。

ActionName is part of the System.Web.Mvc namespacing. ActionName是System.Web.Mvc名称空间的一部分。 It is most likely getting ignored by WebAPI. WebAPI很可能会忽略它。 WebAPI is going to give preference to your Get and Post tags first and foremost. WebAPI首先将优先考虑您的Get和Post标记。

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. 如果您熟悉ASP.NET MVC,则Web API路由与MVC路由非常相似。 The main difference is that Web API uses the HTTP method, not the URI path, to select the action. 主要区别在于Web API使用HTTP方法而不是URI路径来选择操作。 You can also use MVC-style routing in Web API. 您还可以在Web API中使用MVC样式的路由。 This article does not assume any knowledge of ASP.NET MVC. 本文不假定您对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 您可以在以下位置找到有关WebAPI路由的更多信息以及上面的报价: 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. 为了突出显示帮助页面试图告诉您的内容,api是您公开的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. 它将始终尝试使用REST动词(在这种情况下为GET和POST)来标识该点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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