简体   繁体   English

asp.net MVC Web API帮助页面未填充给定路由

[英]asp.net mvc web api help page not populating for given route

I am having difficulties having the help page populate for a web api controller. 我在为Web api控制器填充帮助页面时遇到困难。 The only method in the controller is the following: 控制器中的唯一方法如下:

public HttpResponseMessage Get(string p1,string p2= "blend", string p3 = "blend")

The method appears reflected in the help page with the signature: 该方法将显示在帮助页面上并带有签名:

GET api/mycontroller?p1={p1}&p2={p2}&p3={p3}

However none of my comments flow to the help page. 但是,我的评论都没有流到帮助页面。 For other simple controller Get(string id) the help page works ok. 对于其他简单的控制器Get(string id) ,帮助页面可以正常工作。

I tried adding the following on the WebApiConfig.cs 我尝试在WebApiConfig.cs上添加以下内容

config.Routes.MapHttpRoute( name: "mycontroller", routeTemplate: "api/mycontroller/{p1}/{p2}/{p3}", defaults: new { p1 = RouteParameter.Optional, p2 = RouteParameter.Optional, p3 = RouteParameter.Optional
} );

But still the help page is not getting populated with the comments written for summary, param description or return. 但是,帮助页面仍未填充用于摘要,参数描述或返回的注释。

Instead of trying to solve this with convention based routing in the setup I would use attribute routing . 与其尝试在设置中使用基于约定的路由解决问题,不如使用属性路由

First enable it in the WebApiConfig.cs 首先在WebApiConfig.cs中启用它

config.MapHttpAttributeRoutes();

Then decore the method in the controller with the route 然后用路由装饰控制器中的方法

[HttpGet]
[Route("api/mycontroller")]
public HttpResponseMessage Get1(string p1, string p2= "blend", string p3 = "blend")
//Call this api/mycontroller?p1=valueOne&p2=valueTwo&p3=valueThree

[HttpGet]
[Route("api/mycontroller/{p1}/p2/p3")]
public HttpResponseMessage Get2(string p1,string p2= "blend", string p3 = "blend")
//Call this api/mycontroller/valueOne/valueTwo/valueThree

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

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