Asp.net
表單鍵或值長度限制 2048 超出
我正在使用 asp.net 核心來建構 API。我有一個請求,允許使用者使用此程式碼上傳個人資料圖片
[HttpPost("{company_id}/updateLogo")] public async Task<IActionResult> updateCompanyLogo(IFormFile imgfile,int company_id) { string imageName; // upload file if (imgfile == null || imgfile.Length == 0) imageName = "default-logo.jpg"; else { imageName = Guid.NewGuid() + imgfile.FileName; var path = _hostingEnvironment.WebRootPath + $@"\Imgs\{imageName}"; if (imgfile.ContentType.ToLower().Contains("image")) { using (var fileStream = new FileStream(path, FileMode.Create)) { await imgfile.CopyToAsync(fileStream); } } } . .但它一直返回此異常:
Form key or value length limit 2048 exceeded請求
http://i.imgur.com/25B0qkD.png
更新:
我已經嘗試過這段程式碼,但它不起作用
services.Configure<FormOptions>(options => { options.ValueLengthLimit = int.MaxValue; //not recommended value options.MultipartBodyLengthLimit = long.MaxValue; //not recommended value });
預設情況下,ASP.NET Core 強制執行 2048 內部的鍵/值長度限制
FormReader為常量,並應用FormOptions如下所示:public class FormReader : IDisposable { public const int DefaultValueCountLimit = 1024; public const int DefaultKeyLengthLimit = 1024 * 2; // 2048 public const int DefaultValueLengthLimit = 1024 * 1024 * 4; // 4194304 // other stuff } public class FormOptions { // other stuff public int ValueCountLimit { get; set; } = DefaultValueCountLimit; public int KeyLengthLimit { get; set; } = FormReader.DefaultKeyLengthLimit; public int ValueLengthLimit { get; set; } = DefaultValueLengthLimit; // other stuff }
KeyValueLimit因此,您可以創建一個自定義屬性,通過使用和ValueCountLimit屬性(ValueLengthLimit等等)來明確設置您自己的鍵/值長度限制:[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class RequestSizeLimitAttribute : Attribute, IAuthorizationFilter, IOrderedFilter { private readonly FormOptions _formOptions; public RequestSizeLimitAttribute(int valueCountLimit) { _formOptions = new FormOptions() { // tip: you can use different arguments to set each properties instead of single argument KeyLengthLimit = valueCountLimit, ValueCountLimit = valueCountLimit, ValueLengthLimit = valueCountLimit // uncomment this line below if you want to set multipart body limit too // MultipartBodyLengthLimit = valueCountLimit }; } public int Order { get; set; } // taken from /a/38396065 public void OnAuthorization(AuthorizationFilterContext context) { var contextFeatures = context.HttpContext.Features; var formFeature = contextFeatures.Get<IFormFeature>(); if (formFeature == null || formFeature.Form == null) { // Setting length limit when the form request is not yet being read contextFeatures.Set<IFormFeature>(new FormFeature(context.HttpContext.Request, _formOptions)); } } }動作方法中的使用範例:
[HttpPost("{company_id}/updateLogo")] [RequestSizeLimit(valueCountLimit: 2147483648)] // e.g. 2 GB request limit public async Task<IActionResult> updateCompanyLogo(IFormFile imgfile, int company_id) { // contents removed for brevity }**注意:**如果使用的是最新版本的 ASP.NET Core,請將名為的屬性更改
ValueCountLimit為KeyCountLimit.**更新:**屬性
Order必須包含在屬性類中,因為它是已實現介面的成員IOrderedFilter。類似問題: