繁体   English   中英

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

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

我通过nuget将ASP.NET 4 Web API帮助页面包安装到我的Web Api项目中。 由于某种原因,它不会显示所有api端点。 我有文档设置使用XML。 不知道为什么会这样,任何帮助表示赞赏。

这是一个示例控制器

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

路线

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

谢谢

我想出了问题,这里的问题是,在Web API中没有动作和方法直接映射到动词和参数,更新我的路由到这个修复了问题并且所有路由都显示出来。

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

如果上述解决方案无法解决您的问题 - 请查看并确保您没有定义AcceptVerbs属性及其快捷方式:

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

}

删除其中一个它应该工作。

HTTP方法可能无法使用的另一个原因,无论是作为端点还是未在自动生成的帮助中显示:

该功能可能是private而不是public


会出现:

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

不会出现:

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

对于有不同问题陈述的人,我的回答可能会有所帮助,因为我面临类似的问题,浪费了几个小时。

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

此api端点不会出现在API帮助页面中。 如果您观察到参数名称在路径{id}和方法参数“bookingId”中不同。 两者应该与下面的代码中指定的相同。

[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