你的位置:首页 > 软件开发 > ASP.net > WebAPI接收JSON参数注意事项

WebAPI接收JSON参数注意事项

发布时间:2016-02-04 00:00:13
运行环境:ASP.NET 4.5.2。当我们向GlobalConfiguration.Configuration.MessageHandlers添加一个DelegatingHandler派生类后,很容易发生即使命中了Action,但方法参数值为null的问题。在大多数情况下,我们 ...

WebAPI接收JSON参数注意事项

运行环境:get='_blank'>ASP.NET 4.5.2。

当我们向GlobalConfiguration.Configuration.MessageHandlers添加一个DelegatingHandler派生类后,很容易发生即使命中了Action,但方法参数值为null的问题。

在大多数情况下,我们会在DelegatingHandler派生类里,重写async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)方法。

在方法内,做一些事情,比如说访问日志记录,任意校验,添加Header,修改URL(重写)等工作。

其中最重要需要注意的一点在于request.Content,当我们在方法内访问了request.Content (get)之后,而不对request.Content进行赋值(set)的话,会发生什么呢?

这会导致我们的方法(action)无法获取到客户端Post上来的数据,导致方法参数值为null。

这是为什么呢,这个中原因,我没去深究。

现在附上解决代码:

 1 public class DefaultHandler : DelegatingHandler 2 { 3   protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 4   { 5     request.RequestUri = new Uri(request.RequestUri.ToString()); 6  7     MediaTypeHeaderValue contentType = request.Content.Headers.ContentType; 8  9     if (contentType != null)10     {11       switch (contentType.MediaType)12       {13         case "application/x-www-form-urlencoded":14           {15             NameValueCollection formData = await request.Content.ReadAsFormDataAsync(cancellationToken);16             request.Content = new FormUrlEncodedContent(Correct(formData));17             //TODO:在这里对formData进行业务处理18           }19           break;20         case "multipart/form-data":21           {22             NameValueCollection formData = await request.Content.ReadAsFormDataAsync(cancellationToken);23             request.Content = new FormUrlEncodedContent(Correct(formData));24             //TODO:在这里对formData进行业务处理25           }26           break;27         case "application/json":28           {29             HttpContentHeaders oldHeaders = request.Content.Headers;30             string formData = await request.Content.ReadAsStringAsync();31             request.Content = new StringContent(formData);32             ReplaceHeaders(request.Content.Headers, oldHeaders);33             //TODO:在这里对formData进行业务处理34           }35           break;36         default:37           throw new Exception("Implement It!");38       }39     }40 41     return await base.SendAsync(request, cancellationToken);42   }43 44   private static IEnumerable<KeyValuePair<string, string>> Correct(NameValueCollection formData)45   {46     return formData.Keys.Cast<string>().Select(key => new KeyValuePair<string, string>(key, formData[key])).ToList();47   }48 49   private static void ReplaceHeaders(HttpHeaders currentHeaders, HttpHeaders oldHeaders)50   {51     currentHeaders.Clear();52     foreach (var item in oldHeaders)53       currentHeaders.Add(item.Key, item.Value);54   }55 }

原标题:WebAPI接收JSON参数注意事项

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。