关于web api自动生成的帮助页面,我将不胜感激.
据我所知,如果我返回一个Type,它将自动生成该操作的帮助页面,并附带一个示例.但是,如果我使用HttpResponseMessage而不是可以理解它无法猜测响应将是什么,并且只能对请求参数做出假设.
我使用HttpResponseMessage的原因是因为当它可能不同于200时,建议您指明要返回的状态代码.
那么,能够返回所需状态代码的最佳实践方法是什么,但仍然有帮助页面确定您要返回的类型?
Kir*_*lla 19
对于需要返回HttpResponseMessage的情况,解决方法是使用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)
注意:
在即将发布的版本中,我们引入了一个名为的新属性System.Web.Http.Description.ResponseTypeAttribute,您可以在其中提供System.Type指示响应的实际类型.这样您就可以返回HttpResponseMessage或IHttpActionResult从您的操作中继续操作,并且仍然可以使用HelpPage.
小智 8
我认为属性是一个好主意,所以我已经实现了一个属性,可以帮助其他人,直到你们发布它.
使用属性装饰您的操作:
public class FooController : ApiController
{
[ResponseType(typeof(Bar))]
public HttpResponseMessage Get(string id)
{
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
定义属性:
[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; }
}
Run Code Online (Sandbox Code Playgroud)
定义注册响应类型的方法:
/// <summary>
/// Registers api controller actions which return HttpResponseMessage
/// and include the ResponseType attribute to be populated with web api
/// auto generated help.
/// </summary>
/// <param name="assembly">The assembly to search for</param>
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
将其包含在您的应用程序启动中:
RegisterHelpResponseTypes(typeof(FooController).Assembly);
Run Code Online (Sandbox Code Playgroud)
如果您发现任何问题,请告诉我.
MVC 5具有内置属性来设置响应类型.
更多信息请访问:http: //thesoftwaredudeblog.wordpress.com/2014/01/05/webapi-2-helppage-using-responsetype-attribute-instead-of-setactualresponsetype/
只需使用:
ResponseType(typeof([Your_Class]))]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5113 次 |
| 最近记录: |