ASP.NET Several ways to adjust the size of HTTP request in Core

Time:2020-7-5

1、 Preface

Why ASP.NET Core is a web development platform, because it has a highly extensible request processing pipeline. We can customize this pipeline to meet the HTTP processing requirements in various scenarios. Many features of ASP. Net core application, such as routing, authentication, session, cache, are also customized to achieve message processing pipeline. We can even customize the ASP.NET We create our own web framework on the core platform. In fact, MVC and singalr are two important web frameworks created in this way.

The characteristics of HTTP protocol determine that the working mode of any web application is to listen to, receive and process HTTP requests, and respond to the requests finally. HTTP request processing is a typical application scenario of pipeline design. We customize a message processing pipeline according to the processing flow of HTTP requests, so that the received HTTP request messages flow into the pipeline like water, and each link of the pipeline is processed accordingly once. The result of the processing is also transformed into a message flowing back into the pipeline for processing, and eventually into an HTTP response that is returned to the client.

In general, we do not need to call the size of the HTTP request. Only when uploading some large files or using the HTTP protocol to write large values (such as calling WebService) can we call the maximum HTTP request value.

stay ASP.NET In core 2.0, the default maximum HTTP request size of kestrel and httpsys is 30MB (~ 28.6mib).

If the HTTP request value is greater than this default configuration, the Request.Body.ReadAsync Method, an IOException exception is thrown. If this exception is not caught, the HTTP status code 413 (request entity too large) will be output in kestrel server, while the HTTP status code in httpsys will be 500 (internal server error).

2、 Solutions

stay ASP.NET This request and the core can be configured on a per request basis.

1. MVC solution

MVC core provides us with two feature configuration request sizes:

The requestsizelimit attribute is used to configure the request size of each action. Adjust the request size of myaction to 100000000 bytes


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

Disablerequestsizelimit attribute can be applied to both controller and action to disable the size limit for HTTP requests, which means to set it to unlimited.


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

2. The solution of request context

This scheme is a global configuration scheme, which will affect each request. Of course, a single request can be modified through some flexible configuration. It is configured through the ihttpmax request bodysizefeature feature. The following can be obtained in httpcontext or IOC of application services.


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

The maxrequestbodysize property of the ihttpmaxrequestbodysizefeature feature is nullable < long > type. When set to null, the class is similar to [disablerequestsizelimit] in MVC. The isReadOnly property 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 servers kestrel and httpsys configuration, and the rules are the same as the first two schemes.


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

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

summary

The above is the whole content of this article, I hope that the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message and exchange, thank you for your support to developeppaer.