我正在尝试创建一个具有文件(图像)的模型,但程序在 POST 时不断崩溃。因此,当它像那样崩溃时,我不知道在哪里放置断点进行调试。
这是我的视图模型
CreateNovelViewModel.cs
:
public class CreateNovelViewModel
{
[Required]
public String NovelTitle { get; set; }
[Required]
public String NovelDescription { get; set; }
[Required]
public IFormFile NovelCover { get; set; }
[Required]
public string UserID { get; set; }
[Required]
public int AuthorID { get; set; }
}
NovelsController.cs
:
// GET: Novels/Create
public IActionResult Create()
{
ViewBag.AuthorId = new SelectList(_context.Authors, "AuthorId", "AuthorName");
ViewBag.UserId = new SelectList(_context.Users, "UserId", "UserId");
return View();
}
// POST: Novels/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CreateNovelViewModel novelVM)
{
Novel novel = new Novel();
try
{
if (ModelState.IsValid)
{
byte[] uploadedFile = new byte[novelVM.NovelCover.Length];
novel.NovelTitle = novelVM.NovelTitle;
//novel.NovelCover = novelVM.NovelCover;
novel.NovelDescription = novelVM.NovelDescription;
novel.AuthorId = novelVM.AuthorID;
novel.UserId = novelVM.UserID;
_context.Add(novel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
else
{
ModelState.AddModelError("", "Không thể thêm.Hãy thử lại, nếu vấn đề còn tồn tại, liên hệ người có chuyên môn.");
}
}
catch (RetryLimitExceededException ex)
{
ModelState.AddModelError("", "Không thể thêm. Hãy thử lại, nếu vấn đề còn tồn tại, liên hệ người có chuyên môn.");
}
ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "AuthorName", novelVM.AuthorID);
ViewData["UserId"] = new SelectList(_context.Users, "UserId", "UserId", novelVM.UserID);
return View(novel);
}
Create.cshtml
:
@model WebNovel.Models.ViewModels.CreateNovelViewModel
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Novel</h4>
<hr />
<div class="row">
<div class="col-md-4 p-3">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="NovelTitle" class="control-label"></label>
<input asp-for="NovelTitle" class="form-control" />
<span asp-validation-for="NovelTitle" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NovelDescription" class="control-label"></label>
<input asp-for="NovelDescription" class="form-control" />
<span asp-validation-for="NovelDescription" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NovelCover" class="control-label"></label>
<input asp-for="NovelCover" type="file" class="form-control" />
<span asp-validation-for="NovelCover" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="AuthorID" class="control-label"></label>
<select asp-for="AuthorID" class ="form-control" asp-items="ViewBag.AuthorId"></select>
</div>
<div class="form-group">
<label asp-for="UserID" class="control-label"></label>
<select asp-for="UserID" class ="form-control" asp-items="ViewBag.UserId"></select>
</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>
每当我尝试点击“创建”按钮时,程序就会崩溃。我怀疑它在视图模型中出现了某种错误。
编辑:显示错误:
在航站楼:
The program '[8] WebNovel.exe' has exited with code 4294967295 (0xffffffff).
我试过:
applicationhost.config
他们都没有解决我的问题。
任何帮助将不胜感激。谢谢。
尽管有错误,我注意到你错过了添加 {enctype="multipart/form-data"} 到你的表单,
<form asp-action="Create" enctype="multipart/form-data" >
所以现在表格将随身携带文件。
这可能不是解决方案,但是,由于您有两个同名操作(“创建”),您应该在第一个操作中指定 [HttpGet],就像在第二个操作中设置 [HttpPost] 一样。这应该有助于 ASP.NET 绑定操作。
文件上传使用的磁盘和内存取决于并发文件上传的数量和大小。如果一个应用程序试图缓冲太多的上传,网站会在内存或磁盘空间不足时崩溃。您可以尝试使用较小的文件进行测试,看看它是否有效。
如果你真的有文件过大的问题,可以在action中添加如下属性:
[HttpPost]
[ValidateAntiForgeryToken]
[DisableRequestSizeLimit]
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)]
public async Task<IActionResult> Create(CreateNovelViewModel novelVM)
{
}
有用的链接: