Detailed explanation of several methods of adjusting HTTP request size in ASP. NET Core

  • 2021-10-24 19:22:32
  • OfStack

1. Preface

The reason why ASP. NET Core is an Web development platform is that it has a highly extensible request processing pipeline, and we can customize this pipeline to meet HTTP processing requirements in various scenarios. ASP. NET Core applications have many features, such as routing, authentication, session, caching, etc., which are also implemented by customizing message processing pipelines. We can even create our own Web framework on the ASP. NET Core platform through pipeline customization. In fact, two important Web frameworks, MVC and SingalR, are also created in this way.

The characteristics of HTTP protocol determine that the working mode of any Web application is to monitor, receive and process HTTP requests, and finally respond to the requests. HTTP request processing is a typical application scenario of pipelined design. We customize a message processing pipeline according to the processing flow of HTTP request, and let the received HTTP request message flow into this pipeline like water, and each link that constitutes this pipeline processes it accordingly once. The result of the processing is also converted into a message that flows backwards into the pipeline for processing and ultimately into an HTTP response back to the client.

1, we do not need to call the HTTP request size, only when uploading 1 large files, or using HTTP protocol to write a larger value (such as calling WebService) can we call the HTTP maximum request value.

In ASP. NET Core 2.0, its two hosting servers, Kestrel and HttpSys, default HTTP with a maximum request size of 30MB (~ 28.6 MiB).

If the HTTP request value is greater than this default configuration, an IOException exception is thrown when the Request. Body. ReadAsync method is executed. If this exception is not caught, the HTTP status code 413 (Request Entity Too Large) is output in the Kestrel server, and the HTTP status code in the HttpSys will be 500 (Internal Server Error).

2. Solutions

In ASP. NET Core this configuration can be configured on a global and per-request basis.

1. Solutions for MVC

MVC Core provides us with two feature configuration request sizes:

RequestSizeLimit Attribute, configuring the request size for each Action. Adjust the request size value of MyAction to 100,000,000 bytes as follows.


[HttpPost]
[RequestSizeLimit(100_000_000)]
public IActionResult MyAction([FromBody] MyViewModel data)
{
}

DisableRequestSizeLimit Attribute can be applied to both Controller and Action to disable size limits on HTTP requests, or set to unlimited.


[HttpPost]
[DisableRequestSizeLimit]
public IActionResult MyAction([FromBody] MyViewModel data)
{
}

2. Solutions to the Request Context

This scheme is a global configuration scheme, which will affect each request. Of course, a single request can be modified through a flexible configuration, which is configured through IHttpMaxRequestBodySizeFeature features. The following is obtained in HttpContext, and of course it can also be obtained in IOC of ApplicationServices.


HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 100_000_000;

The MaxRequestBodySize attribute of the IHttpMaxRequestBodySizeFeature feature is Nullable < long > Type, when set to null, the class is intended for [DisableRequestSizeLimit]] in MVC. The IsReadOnly attribute indicates whether the request size in the context can be modified at this time.

3. Global Configuration Solution

The request size is modified by two host server Kestrel and HttpSys configurations, with the same rules as the first two scenarios.


.UseKestrel(options =>
{
 options.Limits.MaxRequestBodySize = null;
}

.UseHttpSys(options =>
{
 options.MaxRequestBodySize = 100_000_000;
}

Summarize


Related articles: