ASP.NET 3.0 MVC 应用在文件/图片上传后崩溃

10
我正在使用 ASP.Net Core 3.0,我想创建一个带有图像的新产品,但是当我选择文件上传中的图像并按下“创建”按钮时,我的应用程序崩溃了。我尝试在控制器上进行调试,但是应用程序在到达控制器之前就崩溃了。在创建操作中,其他所有内容都正常工作。当我注释掉文件输入时,其他所有内容都可以正常工作。 我只想将图像与我的ProductModelVM一起发布,以便我可以在控制器中处理它们。
这是我的ProductPostVM模型:
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace WebshopAppMVC.Models
{
    public class ProductPostVM
    {
        //[JsonPropertyName("id")]
        //public int Id { get; set; }
        [Required]
        [JsonPropertyName("name")]
        public string Name { get; set; }
        [JsonPropertyName("description")]
        public string Description { get; set; }
        [Required]
        [JsonPropertyName("price")]
        public double Price { get; set; }
        [Required]
        [JsonPropertyName("manufacturerId")]
        // A product has one manufacturer
        public int ManufacturerId { get; set; }
        [Required]
        [JsonPropertyName("categories")]
        // products can have many Categories
        public ICollection<int> Categories { get; set; }
        [JsonPropertyName("images")]
        // one product can have many images
        public IEnumerable<IFormFile> Images { get; set; }
    }
}

这是我的Create.cshtml页面:
@model WebshopAppMVC.Models.ProductPostVM
@using System.Text.Json;
@using WebshopAppMVC.Models;
@using Microsoft.AspNetCore.Http;

@{
    ViewData["Title"] = "Create";
    List<ManufacturerVM> manufacturers = JsonSerializer.Deserialize<List<ManufacturerVM>>(@Context.Session.GetString("manufacturers"));
    SelectList manufacturersData = new SelectList(manufacturers, "Id", "Name");
    List<CategoryVM> categories = JsonSerializer.Deserialize<List<CategoryVM>>(@Context.Session.GetString("categories"));
}

<h1>Create</h1>

<h4>ProductPostVM</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            @*<div class="form-group">
        <label asp-for="Id" class="control-label"></label>
        <input asp-for="Id" class="form-control" />
        <span asp-validation-for="Id" class="text-danger"></span>*@
            @*</div>*@
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Description" class="control-label"></label>
                <input asp-for="Description" class="form-control" />
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Price" class="control-label"></label>
                <input asp-for="Price" class="form-control" />
                <span asp-validation-for="Price" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ManufacturerId" class="control-label"></label>
                <select asp-for="ManufacturerId" class="form-control" asp-items=@manufacturersData>
                    <option value="">Please select</option>
                </select>
                <span asp-validation-for="ManufacturerId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Categories" class="control-label"></label>
                <div class="col-md-offset-2 col-md-10">
                    <table>
                        <tr>
                            @{
                                int cnt = 0;

                                foreach (var category in categories)
                                {
                                    if (cnt++ % 3 == 0)
                                    {
                                    @:</tr><tr>
                                    }
                                    @:<td>
                                        <input type="checkbox"
                                               name="Categories"
                                               value="@category.Id"
                                               @(Html.Raw(category.Assigned ? "checked=\"checked\"" : "")) />
                                        @category.Name
                                    @:</td>
                                }
                            @:</tr>
                            }
                        </table>
                    </div>
                </div>
            <div class="form-group">
                <dl>
                    <dt>
                        <label asp-for="Images"></label>
                    </dt>
                    <dd>
                        <input asp-for="Images" type="file" multiple>
                    </dd>
                </dl>
            </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>

    <div>
        <a asp-action="Index">Back to List</a>
    </div>


这是我的控制器中创建操作的代码:
[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(ProductPostVM productPost)
        {
            if (ModelState.IsValid)
            {
                //foreach (string file in Request.)
                //{
                //    var postedFile = Request.Files[file];
                //    postedFile.SaveAs(Server.MapPath("~/UploadedFiles/") + Path.GetFileName(postedFile.FileName));
                //}
                var client = _httpClientFactory.CreateClient();

                var productContent = new StringContent(JsonSerializer.Serialize(productPost), Encoding.UTF8, "application/json");

                HttpResponseMessage httpResponseMessage = await client.PostAsync(new Uri("https://localhost:44352/api/products"), productContent).ConfigureAwait(false);

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    return RedirectToAction(nameof(Index));
                }
            }

            return View(productPost);
        }

这是我的视图的图片: create.cshtml

当您运行程序时,您使用的是哪个浏览器? - Rahul Sharma
我正在使用Brave浏览器,因为您的问题,我在Chrome浏览器中尝试了一下,它可以工作!所以我想我再也不会使用Brave浏览器了 ;) 感谢您的帮助! - ys121
2个回答

24

这种具体行为被归因于浏览器问题,而不是一般的 Visual Studio 问题。根据这篇文章和这篇文章,在使用像该案例中的 BraveYandex 这样的浏览器时,通常会观察到这种行为。有时甚至 Chrome 也会出现这种行为,但这并不是一致的(至少这是我所观察到的)。

可能的解决方案是将浏览器类型更改为使用理想的浏览器,如 Chrome、Firefox 或 Edge。

对于使用 Brave 浏览器的用户,另一种选择是:

关闭(下降)防护可以停止崩溃。您可以通过单击 URL 右侧的盾牌图标来执行此操作。


3
遇到了Brave的问题。想要找到一种方法在某个地方捕获异常,但是没有成功。尝试使用Firefox进行测试确实对我有用。谢谢。 - Jorge Cornejo Bellido

0
你的 <form> 标签使用了错误的 method 属性。 method="get"默认值
尝试:
<form asp-action="Create" enctype="multipart/form-data" method="post">

1
那对我仍然不起作用,没有任何区别,当我进入Visual Studio调试输出时,它显示:程序 '[18948] iisexpress.exe: Program Trace' 已以代码0 (0x0)退出。程序 '[18948] iisexpress.exe' 已以代码-1 (0xffffffff)退出。 - ys121
1
你可能需要清除缓存或使用开发者工具来确保新的更改得以反映。 - karthik kasubha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接