Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of upload restrictions on Mvc files in ASP.NET Core

Shulou Source: shulou.com Published: 2022-06-03 19:16:19 09月25日 Update

Editor to share with you how to solve the problem of upload restrictions on Mvc files in ASP.NET Core, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. brief introduction

In ASP.NET Core MVC, the maximum upload file for file upload defaults to 20MB. If we want to upload some large files, we don't know how to set it up. How should we start without Web.Config?

Second, set the upload file size

1. Application level settin

We need to add the following code to the ConfigureServices method to set the file upload size limit to 60 MB.

Public void ConfigureServices (IServiceCollection services) {servicesConfigure (options = > {optionsMultipartBodyLengthLimit = 6000000;});}

2.Action level settin

In addition to the global settings above, we can also control a single Action by customizing Filter. The Filter code is as follows:

[AttributeUsage (AttributeTargetsClass | AttributeTargetsMethod, AllowMultiple = false, Inherited = true)] public class RequestFormSizeLimitAttribute: Attribute, IAuthorizationFilter, IOrderedFilter {private readonly FormOptions _ formOptions; public RequestFormSizeLimitAttribute (int valueCountLimit) {_ formOptions = new FormOptions () {ValueCountLimit = valueCountLimit};} public int Order {get; set;} public void OnAuthorization (AuthorizationFilterContext context) {var features = contextHttpContextFeatures; var formFeature = featuresGet () If (formFeature = = null | | formFeatureForm = = null) {/ / Request form has not been read yet, so set the limits featuresSet (new FormFeature (contextHttpContextRequest, _ formOptions));}

Because in ASP.NET Core MVC, what is different from the previous version is that the specific functions are encapsulated in various Feature (features), and the HttpContext context is just a container that can be managed by each feature. In this Filter, only Action is intercepted, and the FormFeature (responsible for form submission function) in HttpContext is reset to achieve the purpose of limiting the size of specific Action uploaded files.

The above is all the contents of this article entitled "how to solve the problem of upload restrictions on Mvc files in ASP.NET Core". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: File limit size article problem code content function feature level different maximum upper and lower context not much global single just can pass most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft MySQL Shulou Technology Huawei