skip to content

A building-permit portal on ASP.NET Core 10, hosted in-process by IIS, raises IISServerOptions.MaxRequestBodySize, yet 40 MB plan uploads are still rejected; why?

level: middleimportance: should knowfreq 38%

answer

  1. two servers, two limits
  2. requestLimits maxAllowedContentLength
  3. 30,000,000 bytes in both places
  4. UseIIS copies the IIS value in
  5. a startup warning names the conflict

basics

~20 s

IIS request filtering rejects the upload first: its maxAllowedContentLength defaults to 30,000,000 bytes, and IISServerOptions cannot raise it. Raise requestLimits maxAllowedContentLength in web.config as well; ASP.NET Core logs a warning when its limit exceeds IIS's.

solid answer

~40 s

In-process, a request passes through IIS before ASP.NET Core sees it. IIS's request filtering enforces `maxAllowedContentLength`, 30,000,000 bytes unless configured, so a 40 MB upload is refused by IIS whatever the app says. `UseIIS()` even copies that IIS value into `IISServerOptions.MaxRequestBodySize` at startup. If you then set `MaxRequestBodySize` higher in code, `IISHttpServer` logs a warning that the new value conflicts with `maxAllowedContentLength` and that larger requests will still be rejected by IIS. The fix is to raise both: `<requestLimits maxAllowedContentLength="..." />` under `system.webServer/security/requestFiltering` in the project's `web.config` (publish keeps it), and `IISServerOptions.MaxRequestBodySize` or a per-request `IHttpMaxRequestBodySizeFeature` override. Out-of-process, the second limit is Kestrel's `MaxRequestBodySize`, not `IISServerOptions`.

code

xml · 10 lines
xml
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- IIS's own gate; default 30,000,000 bytes -->
        <requestLimits maxAllowedContentLength="100000000" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

go deeper

for a junior

Recall that IIS and ASP.NET Core each limit request bodies, both at 30,000,000 bytes by default, and that both must allow a large upload.

for a middle

Explain maxAllowedContentLength in web.config, IISServerOptions.MaxRequestBodySize, how UseIIS copies the IIS value, and the per-request feature override.

for a senior

Diagnose which gate fired from the startup warning and silent app logs, keep IIS loose only as far as the largest endpoint needs, and adjust Kestrel instead when out-of-process.

for a principal

Set an upload policy for a public portal that keeps limits strict by default and documents which layer owns each limit across hosting models.

## Two gates, not one An in-process ASP.NET Core app under IIS receives requests through two layers, and each has its own body-size limit: 1. **IIS request filtering**, configured in `web.config` under `system.webServer/security/requestFiltering/requestLimits`, attribute `maxAllowedContentLength`. When it is absent, IIS uses **30,000,000 bytes** (about 28.6 MiB). 2. **ASP.NET Core's in-process server, `IISHttpServer`**, configured through `IISServerOptions.MaxRequestBodySize`, whose own default is also **30,000,000 bytes**. A request must pass both. Raising only the second leaves the first in place, and that is exactly the permit portal's symptom: architects upload 40 MB site plans, the code already allows 100 MB, and IIS still rejects the request before any middleware runs. ## How the two values are connected `UseIIS()`, which `WebApplication.CreateBuilder` calls, reads the IIS configuration when the app starts in-process. ANCM reports `maxAllowedContentLength` (or 30,000,000 when request filtering has no `requestLimits` element) and `UseIIS()`: - sets `IISServerOptions.MaxRequestBodySize` to that IIS value, so by default the two gates agree; - remembers the IIS value, and when the server starts, logs a warning if `MaxRequestBodySize` is larger: "Increasing the MaxRequestBodySize conflicts with the max value for IIS limit maxAllowedContentLength. HTTP requests that have a content length greater than maxAllowedContentLength will still be rejected by IIS..." That warning in the startup log is the fastest diagnosis. ## The fix Put the IIS limit in a `web.config` in the project so every publish carries it (the publish transform keeps your elements and only fills in the ASP.NET Core Module parts): ```xml <configuration> <system.webServer> <security> <requestFiltering> <!-- bytes: 100 MB --> <requestLimits maxAllowedContentLength="100000000" /> </requestFiltering> </security> </system.webServer> </configuration> ``` Then choose how ASP.NET Core applies its own limit: - **Globally**, with `builder.Services.Configure<IISServerOptions>(o => o.MaxRequestBodySize = 100_000_000);`. This raises the limit for every endpoint. - **Per request**, by setting `IHttpMaxRequestBodySizeFeature.MaxRequestBodySize` before the body is read, so only the upload endpoint accepts large bodies. The feature becomes read-only once reading starts. Keeping IIS at the largest value any endpoint needs, and ASP.NET Core strict everywhere except the upload endpoint, gives defence in depth without blocking uploads. ## When the app is hosted out-of-process Out-of-process, `IISServerOptions` is not used at all. The layers become: | Layer | Setting | Default | |---|---|---| | IIS request filtering | `maxAllowedContentLength` | 30,000,000 bytes | | Kestrel, behind ANCM | `KestrelServerLimits.MaxRequestBodySize` | 30,000,000 bytes | Raising `IISServerOptions` in that mode changes nothing, which is a second classic way to "fix" the limit without effect. ## Other IISServerOptions worth knowing | Option | Default | Meaning | |---|---|---| | `MaxRequestBodySize` | the IIS value (30,000,000 by default) | body cap enforced by `IISHttpServer`; `null` removes ASP.NET Core's cap only | | `MaxRequestBodyBufferSize` | 1 MB | how much request body is buffered before back-pressure | | `AllowSynchronousIO` | false | synchronous reads and writes throw unless enabled | | `AutomaticAuthentication` | true | populate `HttpContext.User` from Windows authentication | | `AuthenticationDisplayName` | null | name shown for the Windows scheme | ## Checklist for large uploads under IIS - Raise `maxAllowedContentLength` in the project's `web.config`, in bytes, to the largest upload any endpoint accepts. - Raise ASP.NET Core's limit only where needed: `IISServerOptions` globally, or `IHttpMaxRequestBodySizeFeature` per request before the body is read. - Confirm the hosting model; out-of-process means Kestrel's `MaxRequestBodySize` instead of `IISServerOptions`. - Look for the `maxAllowedContentLength` warning in the startup log after deploying. - Stream large files to storage rather than buffering them, so a higher limit does not become memory pressure. - Re-test with a file just above and just below each limit. ## What the client sees When ASP.NET Core's own limit trips, reading the body throws a `BadHttpRequestException` with status 413 and the app's logs record it. When IIS's limit trips, the request never reaches ASP.NET Core, so the app logs nothing; only IIS's logs show the rejection. Silence in the application log is therefore a hint that the outer gate fired. ## Form uploads add a third limit Multipart form reading and attribute-level limits on actions have their own settings in ASP.NET Core's form and model-binding layer; check those too when uploads go through `IFormFile`.

  • How can you tell from logs which gate rejected an oversized upload?
    If ASP.NET Core's limit fired, the app logs a `BadHttpRequestException` with 413 while the body is read. If IIS request filtering fired, the request never reached the app, so the application log is silent and only the IIS log records the rejection. A startup warning about `maxAllowedContentLength` confirms the mismatch.
  • Does setting IISServerOptions.MaxRequestBodySize to null allow unlimited uploads under IIS?
    No. `null` removes only ASP.NET Core's check; IIS's `maxAllowedContentLength` still rejects larger requests, as the option's own documentation notes. Removing both limits is rarely wise for a public portal anyway.
  • Which limit applies after switching the portal to out-of-process hosting?
    IIS's `maxAllowedContentLength` still applies, and the second gate becomes Kestrel's `KestrelServerLimits.MaxRequestBodySize`, because Kestrel serves the forwarded requests. `IISServerOptions` is not used in that mode.

  • IISServerOptions.MaxRequestBodySize also raises IIS's own request limit.
  • Setting MaxRequestBodySize to null removes every limit under IIS.
  • IISServerOptions controls the limit in out-of-process hosting too.
  • maxAllowedContentLength has no default unless you set it.
  • An IIS rejection always shows up in the app's own logs.