自动生成的帮助页面,返回类型为HttpResponseMessage
关于web api自动生成的帮助页面,我将不胜感激。
据我所知,如果我返回一个Type,它将自动生成该操作的帮助页面并附带一个示例。 但是如果我使用HttpResponseMessage而不是可以理解它无法猜测响应将是什么,并且只能对请求参数做出假设。
我使用HttpResponseMessage的原因是因为当它可能不同于200时,建议您指明要返回的状态代码。
那么,能够返回所需状态代码的最佳实践方法是什么 ,但仍然有帮助页面确定您要返回的类型?
对于需要返回HttpResponseMessage的这些场景,解决方法是使用HelpPage提供的一些帮助程序指示该特定操作的实际返回类型。 您可以在路径Areas\HelpPage\App_Start\HelpPageConfig.cs
找到以下代码
//// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent. //// 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");
注意 :
在即将发布的版本中,我们将引入一个名为System.Web.Http.Description.ResponseTypeAttribute
的新属性,您可以在其中提供一个System.Type
指示响应的实际类型。 这样您就可以从您的操作中返回HttpResponseMessage
或IHttpActionResult
,并且仍然希望HelpPage能够正常工作。
MVC 5具有内置属性来设置响应类型。
更多信息请访问: http : //thesoftwaredudeblog.wordpress.com/2014/01/05/webapi-2-helppage-using-responsetype-attribute-instead-of-setactualresponsetype/
只需使用:
ResponseType(typeof([Your_Class]))]
我认为属性是一个好主意,所以我实现了一个属性,可以帮助别人,直到你们发布它。
使用属性装饰您的操作:
public class FooController : ApiController { [ResponseType(typeof(Bar))] public HttpResponseMessage Get(string id) { // ... } }
定义属性:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class ResponseTypeAttribute : Attribute { public ResponseTypeAttribute(Type type) { if (type == null) { throw new ArgumentNullException("type"); } Type = type; } public Type Type { get; private set; } }
定义注册响应类型的方法:
/// /// Registers api controller actions which return HttpResponseMessage /// and include the ResponseType attribute to be populated with web api /// auto generated help. /// /// The assembly to search for public static void RegisterHelpResponseTypes(Assembly assembly) { var apiControllerTypes = assembly .GetTypes().Where(typeof(ApiController).IsAssignableFrom); foreach (var apiControllerType in apiControllerTypes) { var validActions = apiControllerType.GetMethods() .Where(method => Attribute.IsDefined(method, typeof(ResponseTypeAttribute)) && (method.ReturnType == typeof(HttpResponseMessage))); foreach (var action in validActions) { var responseType = (ResponseTypeAttribute)Attribute .GetCustomAttributes(action) .Single(x => x is ResponseTypeAttribute); var controllerName = apiControllerType.Name.Substring(0, apiControllerType.Name.LastIndexOf("Controller", StringComparison.OrdinalIgnoreCase)); var actionName = action.Name; GlobalConfiguration .Configuration .SetActualResponseType(responseType.Type, controllerName, actionName); } } }
将其包含在您的应用程序开始中:
RegisterHelpResponseTypes(typeof(FooController).Assembly);
如果您发现任何问题,请告诉我。
- 为什么WebApi控制器操作后没有人处理DbContext?
- 在URL中使用斜杠“/”路由参数
- 使Web API身份validation返回401而不是重定向到登录页面
- ASP.NET Web API序列化JSON错误:“自我引用循环”
- ASP.NET核心WebAPI 500 IIS 7.5中的内部错误
- 将json发布到mvc 4 WebApi控制器操作。 为什么不反序列化我的数组呢?
- 如何将OAuth2.0与OData客户端代码生成器集成?
- UseWindowsAzureActiveDirectoryBearerAuthentication如何在validation令牌时起作用?
- 同一解决方案中的MVC和Web Api项目