当我想选择要上传的图像时,服务器会停止,退出代码为-1。
我创建了默认的应用程序ASP.NET Core MVC。
编辑后的索引.cshtml
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<form asp-controller="Home" asp-action="SingleFile"
enctype="multipart/form-data">
<input type="file" name="file"/>
<button type="submit">Submit</button>
</form>
</div>
编辑的HomeController.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using WebApplication2.Models;
namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult SingleFile(IFormFile file)
{
//breakpoint here
return RedirectToAction("Index");
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
应用程序运行完美。但是当我点击";打开文件";它打开文件对话框,然后我选择图像(甚至关闭文件对话框(应用程序崩溃,退出代码为-1。HomeController.cs中的断点从未达到,因为应用程序以前会崩溃。我测试了很多教程,但相同的
控制台输出
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:UsersNikitasourcereposWebApplication2WebApplication2
C:UsersNikitasourcereposWebApplication2WebApplication2binDebugnet5.0WebApplication2.exe (процесс 15128) завершил работу с кодом -1.
UPD:即使我留下html代码(删除asp-…标签和@(
更新的index.cshtml
<div class="text-center">
<form>
<input type="file"/>
<button type="submit">Submit</button>
</form>
</div>
应用程序的行为与相同
- 添加方法="张贴";到表单
- 为控制器操作添加[HttpPost]
在没有调试器的情况下通过CTRL+F5运行应用程序