title: Request.Body changes when a parameter is added to the route
body: <p>I am working on an ASP.NET Core API that processes multipart POST requests.</p>
  
  <p>Today I tried to add a parameter to the route, which triggered an <code>IOException</code> (Unexpected end of stream) when trying to call <code>ReadNextSectionAsync</code> on my <code>MultipartReader</code>.</p>
  
  <p>I dumped (using <a href="https://github.com/lassevk/ObjectDumper" rel="nofollow noreferrer">ObjectDumper</a>) the <code>MultipartReader</code> to the log in both cases and the only difference (besides the boundary obviously) is a field of the inner stream. The stream is the <code>Request.Body</code> that I'm passing directly from the controller.</p>
  
  <p>I'll show this difference with a minimal example:</p>
  
  <pre class="lang-cc prettyprint-override"><code>[Route("foo/")]
  public class FooController : Controller
  {
      private readonly ILogger&lt;FooController&gt; _log;
  
      public FooController(ILoggerFactory loggerFactory)
      {
          _log = loggerFactory.CreateLogger&lt;FooController&gt;();
      }
  
      [HttpPost("1/bar")]
      public void Foo()
      {
          _log.LogInformation(Request.Body.DumpToString("Works"));
      }
  }
  </code></pre>
  
  <p>This logs the following:</p>
  
  <pre class="lang-none prettyprint-override"><code>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]
           }
        }
     }
  }
  </code></pre>
  
  <p>However, if I do this:
  </p>
  
  <pre><code>[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"));
      }
  }
  </code></pre>
  
  <p>The <code>_inputLength</code> field has a value of 0.
  Then when I create a <code>MultipartReader</code> with this stream it fails when calling <code>ReadNextSectionAsync</code> for the first time.</p>
  
  <p>This looks a bit like an ASP.NET Core bug to me but I'm new to ASP.NET so I might be missing something important!</p>