本文介绍了asp.net MVC Web API帮助页面未填充给定路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难为Web api控制器填充帮助页面.控制器中唯一的方法如下:

I am having difficulties having the help page populate for a web api controller. 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}

但是,我的评论都没有流到帮助页面.对于其他简单控制器Get(string id),帮助页面可以正常工作.

However none of my comments flow to the help page. For other simple controller Get(string id) the help page works ok.

我尝试在WebApiConfig.cs上添加以下内容

I tried adding the following on the WebApiConfig.cs

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

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.

推荐答案

我将使用属性路由.

首先在WebApiConfig.cs中启用它

First enable it in the 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

这篇关于asp.net MVC Web API帮助页面未填充给定路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:55