简体   繁体   English

ASP.NET Web API帮助页面包没有获得所有api端点

[英]ASP.NET Web API Help Page package does not get all api endpoints

I installed ASP.NET 4 Web API Help Page package via nuget to my Web Api project. 我通过nuget将ASP.NET 4 Web API帮助页面包安装到我的Web Api项目中。 For some reason it does not display all the api endpoints. 由于某种原因,它不会显示所有api端点。 I have the documentation set to use XML. 我有文档设置使用XML。 Not sure why this is happening, any help is appreciated. 不知道为什么会这样,任何帮助表示赞赏。

Here is an example controller 这是一个示例控制器

    public class ProductController : BaseController
    {
        // GET api/Product/Get/5/43324
        [AcceptVerbs("GET")]
        public ApiProduct Get(int id, [FromUri]int productId)
        {
             //// logic
        }
   }

routes 路线

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

Thanks 谢谢

I figured out the issue, the problem here is, In Web API there is no action and methods are mapped to the verb and arguments directly, Updating my route to this fixed the problem and all the routes show up. 我想出了问题,这里的问题是,在Web API中没有动作和方法直接映射到动词和参数,更新我的路由到这个修复了问题并且所有路由都显示出来。

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

If the above solution doesn't solve your problem - take a look and make sure you aren't defining BOTH the AcceptVerbs attribute and its shortcut: 如果上述解决方案无法解决您的问题 - 请查看并确保您没有定义AcceptVerbs属性及其快捷方式:

public class ProductController : BaseController
{
    // GET api/Product/Get/5/43324
    [AcceptVerbs("GET")]
    [HttpGet]
    public ApiProduct Get(int id, [FromUri]int productId)
    {
         //// logic
    }

} }

Remove one of them and it should work. 删除其中一个它应该工作。

Another reason why a HTTP method might not be available, either as an endpoint or not showing up in the autogenerated help: HTTP方法可能无法使用的另一个原因,无论是作为端点还是未在自动生成的帮助中显示:

The function might be private and not public . 该功能可能是private而不是public


Will show up: 会出现:

[HttpGet]
[Route("api/projects")]
public IHttpActionResult GetCount()
{
    return Ok(db.Projects.Count());
}

Won't show up: 不会出现:

[HttpGet]
[Route("api/projects")]
private IHttpActionResult GetCount()
{
    return Ok(db.Projects.Count());
}

My answer might be helpful for someone with different problem statement, As I faced similar sort of issue and wasted few hours. 对于有不同问题陈述的人,我的回答可能会有所帮助,因为我面临类似的问题,浪费了几个小时。

[PUT("Update/booking/{id}")]
public bool Put(id bookingId, [FromBody] BookingEntity bookingEntity)

This api endpoint won't appear in API help page. 此api端点不会出现在API帮助页面中。 If you observe parameter name is different in route that is {id} and method parameters "bookingId". 如果您观察到参数名称在路径{id}和方法参数“bookingId”中不同。 Both should be same as specified in below code. 两者应该与下面的代码中指定的相同。

[PUT("Update/booking/{id}")]
public bool Put(id id, [FromBody] BookingEntity bookingEntity)

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

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