【问题标题】:Data posted via form hangs application通过表单发布的数据挂起应用程序
【发布时间】:2019-03-09 08:11:58
【问题描述】:

我有这样的简单控制器方法:

[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Add(InputModel input, OtherInputModel[] input2)
{
    (...)
}

它从下面这个表单接收数据,但问题是当那个简单的端点 收到OtherInputModel[]然后点击“提交”后挂断

没有任何错误或任何东西 - 它只是在尝试输入该控制器方法时卡住了,即使我没有附加图像。

OtherInputModel[] 更改为 string[] “解决”它,所以仍然可以访问该方法,但我想发送复杂模型 []

<form asp-action="Add" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label class="control-label">Title</label>
        <input name="input.Title" class="form-control" value="@Model.Title" />
        <span class="text-danger"></span>
    </div>

    <div class="form-group">
        <label class="control-label">Body</label>
        <input name="input.Body" class="form-control" value="@Model.Body" />
        <span class="text-danger"></span>
    </div>

    <div class="form-group">
        <div class="col-md-10">
            <p>Upload one image ("jpg", "jpeg", "png", "bmp") using this form:</p>
            <input type="file" name="input.Image" />
        </div>
    </div>

    // here's probably the problem

    @for (int i = 0; i < 4; i++)
    {
        <div class="form-group">
            <label class="control-label"></label>
            <input name="input2[].Text" class="form-control" />
            <span class="text-danger"></span>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                <p>Upload one image ("jpg", "jpeg", "png", "bmp") using this form:</p>
                <input type="file" name="input2[].Image" />
            </div>
        </div>
    }

    <div class="form-group">
        <input type="submit" value="Create" class="button" />
    </div>
</form>

这是我的模型:

public class InputModel
{
    public string Title { get; set; }
    public string Body { get; set; }
    public IFormFile Image { get; set; }
}

public class OtherInputModel
{
    public string Text { get; set; }
    public IFormFile Image { get; set; }
}

我也尝试过像这样在我的表单中使用索引

<input name="input2[@i].Text" class="form-control" />

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc


    【解决方案1】:

    这似乎是 ASP.NET Core 2.2 中的一个愚蠢的错误。与前端无关,它与一个动作需要包含IFormFile 的复杂对象数组和模型绑定进入无限循环(但它在 ASP.Core 2.1 中工作正常)这一事实有关。

    这里是和issue 在 github 上关于这个错误。它已经关闭,看起来该错误已针对 ASP.NET Core 3 修复。

    但是有一些建议的解决方法。

    1。 使用objectName.Index 名称和索引值放置隐藏输入

    <input type="hidden" name="otherinput.Index" value="0" />
    <input type="file" name="otherinput[0].Image" />
    

    工作正常,但如果有人在没有otherinput.Index 输入的情况下提出请求(通过邮递员或在提交前删除input),应用程序将像没有此解决方法一样挂起。所以我会说这是非常脆弱和不可接受的。

    2。 将IFormFile 包装成一个类

    public class OtherInputModel
    {
        public string Text { get; set; }
    
        public string Body { get; set; }
    
        public FileHolder Holder { get; set; }
    }
    
    public class FileHolder
    {
        public IFormFile Image { get; set; }
    }
    

    和标记

    <input name="otherinput[0].Text" class="form-control" value="create btn" />
    <!-- second line won't work by itself, first line with Text or Body property is required -->
    <input type="file" name="otherinput[0].Holder.Image" />
    

    工作正常,但人们说issue 中描述的可能存在内存泄漏。

    【讨论】:

    • 什么?我都做了 - 使用的参数名称和使用的索引。无论如何这不是问题,因为如果在最坏的情况下我的参数将为空,而不是挂起的应用程序。
    • @tezzly 将您问题中的 &lt;input name="OtherInputModel[@i].Text" class="form-control" /&gt; 与我的回答进行比较
    • Ops,很抱歉,我在将代码重写为 SO 时失败了。我正在使用参数名称,我什至尝试使用索引,但问题是应用程序只是挂断了(请求试图在浏览器中发送,应用程序甚至没有在打开 { 的方法处命中断点 - 没有错误,没有任何东西。)如果 name 错误,那么我的参数将是 null 但应用程序会“正常”运行
    • 不确定你的应用会发生什么,但我测试了这段代码,所有值都绑定得很好。你在不同的浏览器上试过了吗?
    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 2023-02-18
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多