2016-10-03 9 views
1

私はマルチパートPOSTリクエストを処理するASP.NET Core APIを開発中です。Request.Bodyは、パラメータがルートに追加されたときに変更されます

今日、私はをMultipartReaderにコールしようとしたときに、IOException(予期せずストリームの終了)を引き起こしたルートにパラメータを追加しようとしました。

どちらの場合も、MultipartReaderをダンプ(ObjectDumperを使用)してログに記録しましたが、唯一の違いは明らかに境界以外にも内部ストリームのフィールドです。ストリームはコントローラから直接渡しているRequest.Bodyです。

私は、最小限の例で、この違いを紹介します:

[Route("foo/")] 
public class FooController : Controller 
{ 
    private readonly ILogger<FooController> _log; 

    public FooController(ILoggerFactory loggerFactory) 
    { 
     _log = loggerFactory.CreateLogger<FooController>(); 
    } 

    [HttpPost("1/bar")] 
    public void Foo() 
    { 
     _log.LogInformation(Request.Body.DumpToString("Works")); 
    } 
} 

これは以下を記録します。

INFO: #1: Works [Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameRequestStream] 
{ 
    properties { 
     CanRead = True [System.Boolean] 
     CanSeek = False [System.Boolean] 
     CanWrite = False [System.Boolean] 
     #2: Length = TargetInvocationException: Exception has been thrown by the target of an invocat (+4 chars) [System.Reflection.TargetInvocationException] 
     #3: Position = TargetInvocationException: Exception has been thrown by the target of an invocat (+4 chars) [System.Reflection.TargetInvocationException] 
     CanTimeout = False [System.Boolean] 
     #4: ReadTimeout = TargetInvocationException: Exception has been thrown by the target of an invocat (+4 chars) [System.Reflection.TargetInvocationException] 
     #5: WriteTimeout = TargetInvocationException: Exception has been thrown by the target of an invocat (+4 chars) [System.Reflection.TargetInvocationException] 
    } 
    fields { 
     #6: _body [Microsoft.AspNetCore.Server.Kestrel.Internal.Http.MessageBody+ForContentLength] 
     { 
     properties { 
      RequestKeepAlive = True [System.Boolean] 
     } 
     fields { 
      _contentLength = 242 [System.Int64] 
      _inputLength = 242 [System.Int64] 
     } 
     } 
     _state = Open [Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameStreamState] 
     { 
     fields { 
      value__ = 0 [System.Int32] 
     } 
     } 
    } 
} 

しかし、私はこれを行う場合:

[Route("foo/")] 
public class FooController : Controller 
{ 
    // ... 
    // Same logging setup 
    // ... 

    [HttpPost("{id:int}/bar")] // This doesn't actually need to change 
    public void Foo(int id) 
    { 
     _log.LogInformation(Request.Body.DumpToString("Doesn't work")); 
    } 
} 

_inputLengthフィールドの値は0です。 次に、 MultipartReaderこのストリームで初めてReadNextSectionAsyncを呼び出すと失敗します。

これはASP.NET Coreのバグと似ていますが、私はASP.NETを初めて使用しているので、何か重要なものが欠落している可能性があります。

答えて

1

ルートにパラメータを追加すると、要求本体を読み取るASP.NETモデルバインディングがトリガされるようです。その後、私はそれを読むことができません。

これは、2つのオプションが私の葉:

  1. 使用ファイルを読み取るために結合モデルだけでなく
  2. は全く結合モデルを使用してパラメータを得るための別の方法を見つけることができません。

    [HttpPost("{id:int}/bar")] 
    public void Foo() 
    { 
        int id = Int32.Parse((string)RouteData.Values["id"]); 
    
        // ... 
    } 
    

は、私のような何かを書いて、2番目のオプションを選択しました

関連する問題