Larger uploads on dynamic routes in ASP.NET MVC

| 2 min. (256 words)

We recently had an issue here at Raygun where we needed to increase the size of uploads that we allowed.

In ASP.NET this is a simple matter of defining a location and increasing the size of the maxRequestLength and also adding a section to allow IIS to accept requests above its default size of 30 MB.

<configuration>
  <location path="upload/all/the/things">
    <system.web>
      <!-- This is set in bytes -->
      <httpRuntime maxRequestLength="66560" />
    </system.web>
  </location>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- use a different measurement scale here (kilobytes) #wat -->
        <requestLimits maxAllowedContentLength="68157440" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

This works fine if your route does not contain any dynamic data. Unfortunately, if it does then you are out of luck, as the location path attribute only supports static paths.

We needed to preserve the existing URL as this is used by external clients. In order to accomplish this we can use IIS URL rewrite rules to rewrite incoming requests to our dynamic route and rewrite them to a static one with the dynamic sections appended via query string.

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="rewrite the upload path" enabled="true">
          <match url="^dynamic/(.*)/routes/are/cool$" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
            <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST"
                ignoreCase="true" />
          </conditions>
          <action type="Rewrite" url="upload/all/the/things?identifier={R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

The last piece of the puzzle is to get the controller action in question to respond to both routes. Once this is done you now have a MVC route that can upload more than the average bear, while also preserving the dynamic properties that you know and love.

Keep blasting those errors!