2013-09-30 6 views
5

POST、PUT、PATCHリクエストでContent-Typeの値を検証しようとしていますが、content-type句を忘れたとき、のようなタイプ: "Content-Type:Foo"。
私が送っリクエストのコンテンツタイプを確認する

500 Internal Server Error 
No MediaTypeFormatter is available to read an object of type 'MyClassDto' from content with media type 'text/css'. 

これは私のコードです:

public class ContentTypeFilter : IActionFilter 
{ 
    private readonly List<MediaTypeHeaderValue> _suport; 

    /// <summary /> 
    public ContentTypeFilterAttribute() 
    { 
     _suport = new List<MediaTypeHeaderValue>(); 

     foreach (var formatter in GlobalConfiguration.Configuration.Formatters.ToArray()) 
     { 
      _suport.AddRange(formatter.SupportedMediaTypes); 
     } 
    } 

    public bool AllowMultiple { get { return false; } } 

    public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation) 
    { 
     var metodos = new List<string> { "POST", "PUT", "PATCH" }; 

     if (actionContext.Request.Content != null) 
     { 
      if (metodos.Contains(actionContext.Request.Method.Method.ToUpperInvariant())) 
      { 
       MediaTypeHeaderValue contentType = actionContext.Request.Content.Headers.ContentType; 

       if (contentType == null || !_suport.Any(x => x.MediaType.Equals(contentType.MediaType))) 
       { 
        return CreateResponse(actionContext.Request, "Invalid Content-Type"); 
       } 
      } 
     } 

     return continuation(); 
    } 

    private static Task<HttpResponseMessage> CreateResponse(HttpRequestMessage request, string mensagem) 
    { 
     var tsc = new TaskCompletionSource<HttpResponseMessage>(); 
     var response = request.CreateResponse(HttpStatusCode.UnsupportedMediaType); 
     response.ReasonPhrase = mensagem; 
     response.Content = new StringContent(mensagem); 
     tsc.SetResult(response); 

     return tsc.Task; 
    } 

コンテンツ・タイプを検証し、エラー415を返すために別の方法があり、「コンテンツタイプ:テキスト/ cssのは、」私はこれを取得コンテンツがXMLまたはJSONでない場合

答えて

3

私は良い解決策を見つけましたhere

public class ContentTypeFilter : DelegatingHandler 
{ 
    private readonly List<MediaTypeHeaderValue> _suport; 

    /// <summary /> 
    public ContentTypeFilter() 
    { 
     _suport = new List<MediaTypeHeaderValue>(); 

     foreach (var formatter in GlobalConfiguration.Configuration.Formatters.ToArray()) 
     { 
      _suport.AddRange(formatter.SupportedMediaTypes); 
     } 
    } 

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 
    { 
     var metodos = new List<string> { "POST", "PUT", "PATCH" }; 

     if (request.Content != null) 
     { 
      if (metodos.Contains(request.Method.Method.ToUpperInvariant())) 
      { 
       MediaTypeHeaderValue contentType = request.Content.Headers.ContentType; 

       // Nas configurações não possui o Charset aceito. 
       if (contentType == null || !_suport.Any(x => x.MediaType.Equals(contentType.MediaType))) 
       { 
        return Task<HttpResponseMessage>.Factory.StartNew(() => CreateResponse(request, "Suported content-types: " + string.Join(", ", _suport.Select(x => x.ToString())))); 
       } 
      } 
     } 
     return base.SendAsync(request, cancellationToken); 
    } 

    private static HttpResponseMessage CreateResponse(HttpRequestMessage request, string mensagem) 
    { 
     var response = request.CreateResponse(HttpStatusCode.UnsupportedMediaType); 
     response.ReasonPhrase = mensagem; 
     response.Content = new StringContent(mensagem); 

     return response; 
    } 
} 
:私が欲しいものを得るためにいくつかの変更により

関連する問題