我正在查看WebAPI 2的文档,对于构建动作结果的方式我感到非常失望.我真的希望有更好的方法.
所以文件说我可以返回这些:
**void** Return empty 204 (No Content) **HttpResponseMessage** Convert directly to an HTTP response message. **IHttpActionResult** Call ExecuteAsync to create an HttpResponseMessage,then convert to an HTTP response message. **Other type** Write the serialized return value into the response body; return 200 (OK).
我没有看到一个干净的方式来返回一个包含自定义HTTP状态代码,自定义标头和自动协商内容的项目数组.
我想看到的就是这样
public HttpResult<Item> Post() { var item = new Item(); var result = new HttpResult<Item>(item,HttpStatusCode.Created); result.Headers.Add("header","header value"); return result; }
这样我可以浏览一个方法,并立即看到返回的内容,并修改状态代码和标题.
我发现最接近的事情是NegotiatedContentResult&T,具有奇怪的签名(为什么需要一个控制器的实例?),但是没有办法设置自定义标头?
有没有更好的办法 ?
解决方法
我不认为web-api的设计人员不愿意使用控制器方法来处理头文件.
设计模式似乎是使用 DelegatingHandler,ActionFilterAttribute和ApiController的ExecuteAsync可覆盖方法来处理身份验证和响应格式.
设计模式似乎是使用 DelegatingHandler,ActionFilterAttribute和ApiController的ExecuteAsync可覆盖方法来处理身份验证和响应格式.
那么也许您的消息内容谈判逻辑应该在那里处理?
但是,如果您绝对需要从控制器方法中控制标题,则可以进行一些设置以使其起作用.
为此,您可以创建自己的DelegateHandler,从“内部”响应标头转发所选标题:
public class MessageHandlerBranding : DelegatingHandler { protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) { var response = await base.SendAsync(request,cancellationToken); //If we want to forward headers from inner content we can do this: if (response.Content != null && response.Content.Headers.Any()) { foreach (var hdr in response.Content.Headers) { var keyUpr = hdr.Key.ToUpper(); //Response will not tolerate setting of some header values if ( keyUpr != "CONTENT-TYPE" && keyUpr != "CONTENT-LENGTH") { string val = hdr.Value.Any() ? hdr.Value.FirstOrDefault() : ""; response.Headers.Add(hdr.Key,val); } } } //Add our branding header to each response response.Headers.Add("X-Powered-By","My product"); return response; } }
然后,您在Web-api配置中注册此处理程序,通常位于GlobalConfig.cs文件中.
config.MessageHandlers.Add(new MessageHandlerBranding());
您还可以为响应对象编写自己的自定义类,如下所示:
public class ApiQueryResult<T> : IHttpActionResult where T : class { public ApiQueryResult(HttpRequestMessage request) { this.StatusCode = HttpStatusCode.OK; ; this.HeadersToAdd = new List<MyStringPair>(); this.Request = request; } public HttpStatusCode StatusCode { get; set; } private List<MyStringPair> HeadersToAdd { get; set; } public T Content { get; set; } private HttpRequestMessage Request { get; set; } public void AddHeaders(string headerKey,string headerValue) { this.HeadersToAdd.Add(new MyStringPair(headerKey,headerValue)); } public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) { var response = this.Request.CreateResponse<T>(this.StatusCode,this.Content); foreach (var hdr in this.HeadersToAdd) { response.Content.Headers.Add(hdr.key,hdr.value); } return Task.FromResult(response); } private class MyStringPair { public MyStringPair(string key,string value) { this.key = key; this.value = value; } public string key; public string value; } }
并在控制器中使用它:
[HttpGet] public ApiQueryResult<CustomersView> CustomersViewsRow(int id) { var ret = new ApiQueryResult<CustomersView>(this.Request); ret.Content = this.BLL.GetOneCustomer(id); ret.AddHeaders("myCustomHkey","myCustomValue"); return ret; }