我使用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);
}
}
}
.
.
Run Code Online (Sandbox Code Playgroud)
但它不断返回此异常: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 = …Run Code Online (Sandbox Code Playgroud) 此问题出现在.net core 3.1 MVC网站中。
我无法将 POST 绑定到控制器操作(参数始终为空)。数据是从数据库加载的,并且是一个大型的递归结构。如果我删除数据库中的几百行 JSON(大约 2500 行),它将绑定正常。
GET 显示完美。
即使当我将 Action 方法参数从 ViewModel 更改为 IFormCollection 时,它仍然显示为 null。这里有一些我没有意识到的限制吗?如果大小是问题,是否有更好的方法来发布数据?
父视图
<form id="frmAdditionalCodes" name="frmAdditionalCodes" method="post">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Code)
</th>
<th>
@Html.DisplayNameFor(model => model.FullName)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td style="width:50px">
@Html.DisplayFor(modelItem => Model.Code)
<input asp-for="@Model.Code" class="form-control" style="display:none" />
</td>
<td style="width:50px">
@Html.DisplayFor(modelItem => Model.FullName)
<input asp-for="@Model.FullName" class="form-control" style="display:none" />
</td>
<td style="width:50px">
@Html.DisplayFor(modelItem => Model.Description)
<input asp-for="@Model.Description" class="form-control" style="display:none" …Run Code Online (Sandbox Code Playgroud) 将超过 1024 个项目的数组提交给控制器(当前为 2,500 个项目)时出现异常。似乎您可以提交的项目数量上限为 1024。
它似乎是在 MvcOptions 中设置的,但是我使用的是 .Net Core 3.0 并使用端点路由,因此我无法通过 UseMVC 访问 MvcOptions。
我怎样才能提高这个限制?
我之前通过添加辅助属性提高了限制,如下所示。但是我不确定我需要在哪里设置这个特定限制 - 它似乎不是 HttpContext.Features 的一部分。
public void OnAuthorization(AuthorizationFilterContext context)
{
var features = context.HttpContext.Features;
var formFeature = features.Get<IFormFeature>();
if (formFeature == null || formFeature.Form == null)
{
features.Set<IFormFeature>(new FormFeature(context.HttpContext.Request, _formOptions));
}
}
Run Code Online (Sandbox Code Playgroud)