Ami*_*ava 14 documentation asp.net-web-api
我正在为所有api响应使用泛型返回类型:
public HttpStatusCode statusCode { get; set; }
public string error { get; set; }
public IDictionary<string, string> errorfor { get; set; }
public T result { get; set; }
Run Code Online (Sandbox Code Playgroud)
在API中:
/// <summary>
/// GET Order API
/// </summary>
/// <returns> return list of orders {Order} </returns>
public HttpResponseMessage Get(){
var response = new BaseResponseMessage<IList<Order>>();
//some more codes
response.result = orders;
return Request.CreateResponse(HttpStatusCode.OK, response);
}
Run Code Online (Sandbox Code Playgroud)
当然,我的API帮助页面不会在示例响应正文中显示Order.是否可以配置帮助页面生成器以显示泛型类型?谢谢!
小智 18
Web API 2.1帮助页面现在执行此操作:http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-21#help-page
您还可以检查使用新的ResponseTypeAttribute返回标准HttpResponseMessage :
/// <summary>
/// GET Order API
/// </summary>
[ResponseType(typeof(List<Order>))]
public HttpResponseMessage Get()
{
var orders = new List<Order>();
return Request.CreateResponse(HttpStatusCode.OK, orders);
}
Run Code Online (Sandbox Code Playgroud)
仅使用您拥有的签名,HelpPage 就不可能猜测操作的返回类型是什么。如您所知,HelpPage 生成不会在正常请求期间发生,而是依赖于静态信息。
不过,有一个解决方法。您可以查看以下带注释的代码,其中Areas\HelpPage\App_Start\HelpPageConfig.cs允许您为操作指定特定的返回类型。
//// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
//// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
//config.SetActualResponseType(typeof(string), "Values", "Post");
Run Code Online (Sandbox Code Playgroud)
我知道这种解决方法对您来说太麻烦了,因为您提到对您的所有操作都使用这种签名。
您可以通过创建一个自定义属性来扩展 HelpPage,该属性通知返回类型并在操作上装饰它。然后,您可以修改已安装的 HelpPage 代码来查找这些属性。
| 归档时间: |
|
| 查看次数: |
6265 次 |
| 最近记录: |