2011-12-17 6 views
2

リクエストのURLに応じて、さまざまな形式の種類を出力しようとしています。 Preview5まで私はMediaTypeFormatters OnWriteToStream-方法でURIを取得するには、次のでしたMediaTypeFormatterのURIを要求します。

var requestUri = OperationContext.Current 
           .IncomingMessageHeaders 
           .To; 

しかしPreview6でOperationContext.Currentプロパティは常にnullです。おそらく、フォーマッタが別のスレッドで実行されるからです。では、MediaTypeFormatterでURIを取得する正しい方法は何ですか?または、引数として要求を持つMediaTypeFormatterの代替手段がありますか?

ありがとうございます。

よろしく

...

ヨアヒム

答えて

0

完全を期すために、私は、我々はまた、弊社のWeb APIのためMediaTypeFormatterと、この問題が発生したが、我々は単にHttpContext.Current.Request.Urlを使用して代わりにOperationContext.Currentを通過することによってそれを解決した後の溶液

public class RazorHtmlHandler : HttpOperationHandler<HttpResponseMessage, HttpResponseMessage> 
{ 
    public static readonly String OUTPUT_PARAMETER_NAME = "response"; 
    public static readonly MediaTypeWithQualityHeaderValue HTML_MEDIA_TYPE = new MediaTypeWithQualityHeaderValue("text/html"); 

    public const String DEFAULT_TEMPLATE_NAME = "index.cshtml"; 
    public const String DEFAULT_TEMPLATE_EXTENSION = ".cshtml"; 
    public const String DEFAULT_RAZOR_NAME = "_RazorHtmlProcessor_Template"; 

    public RazorHtmlHandler() : base(OUTPUT_PARAMETER_NAME) 
    { } 

    protected override HttpResponseMessage OnHandle(HttpResponseMessage response) 
    { 
     var request = response.RequestMessage; 
     var accept = request.Headers.Accept; 

     if (!accept.Contains(HTML_MEDIA_TYPE)) 
      return response; 

     var buffer = new StringBuilder(); 
     var currentContent = response.Content as ObjectContent; 

     try 
     {     
      var template = LoadTemplateForResponse(request.RequestUri, currentContent); 
      var value = ReadValueFormObjectContent(currentContent); 

      buffer.Append(InvokeRazorParse(template, value)); 
     } 
     catch (Exception ex) 
     { 
      throw new HttpResponseException(HttpStatusCode.InternalServerError); 
     } 

     response.Content = new StringContent(buffer.ToString(), 
              Encoding.UTF8, 
              HTML_MEDIA_TYPE.MediaType); 
     return response; 
    } 
    ... 
} 
0

hereを示すようにあなたがUriFormatExtensionMessageChannel/OperationHandlerを使用することができます。

+0

ハ、私は既に投稿したあなたの時間を読みましたが、類推を見ることができませんでした。ヒントありがとう! –

関連する問題