How to adjust the HTTP request size in ASP. NET Core: asp. netcore

Source: Internet
Author: User

How to adjust the HTTP request size in ASP. NET Core: asp. netcore

I. Preface

The reason why ASP. NET Core is called a Web development platform is that it has a highly scalable request processing pipeline. We can customize this pipeline to meet HTTP processing needs in various scenarios. Many features of ASP. NET Core applications, such as routing, authentication, session, cache, and so on, are also customized to implement the message processing pipeline. We can even use pipelines to create our own Web framework on the ASP. NET Core platform. In fact, the two important Web frameworks MVC and SingalR are also created in this way.

The characteristics of the HTTP protocol determine that the way any Web application works is to listen, receive, and process HTTP requests, and finally respond to the requests, HTTP request processing is a typical scenario of pipeline design. We have customized A Message Processing Pipeline Based on the processing process of the HTTP request, so that the received HTTP request messages can flow into the pipeline like water, each part of the pipeline is processed at a time. The processing result is also converted into a message reversely flowing into the pipeline for processing, and finally into an HTTP Response to the client.

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

In ASP. NET Core 2.0, the maximum HTTP request size of its two host servers Kestrel and HttpSys is 30 MB by default (~ 28.6 MiB ).

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

Ii. Solutions

In ASP. NET Core, this configuration can be configured based on the global configuration and each request.

1. MVC Solution

MVC Core provides two features for us to configure the request size:

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

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

DisableRequestSizeLimit Attribute can be applied to the Controller and Action at the same time to disable the size limit on HTTP requests. In other words, it is set to unlimited.

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

2. Request context Solution

This scheme is a global configuration scheme that affects each request. Of course, you can also modify a single request through some flexible configurations. It is configured through the IHttpMaxRequestBodySizeFeature feature. The following is obtained in HttpContext. Of course, it can also be obtained in IOC of ApplicationServices.

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

The maxrequestbodysizefeature attribute of the ihttpmaxrequestbodysize feature is of the Nullable <long> type. When it is set to null, the class is intended to be [DisableRequestSizeLimit] in MVC. The IsReadOnly attribute indicates whether the request size in the context can be modified.

3. Global configuration Solution

The request size is modified using the Kestrel and HttpSys configurations of the two host servers. The rules are the same as those of the previous two solutions.

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

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.