C# Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常
Posted
技术标签:
【中文标题】C# Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常【英文标题】:C# Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request 【发布时间】:2022-01-17 02:39:48 【问题描述】:我在这段代码中需要帮助,有些错误是不对的,哈哈……我正在尝试在 InMemory 数据库中运行,但没有成功。我正在使用
Microsoft.AspNetCore.App 5.0.12
Microsoft.AspNetCore.App 6.0.0
Microsoft.NETCore.App 5.0.12
Microsoft.NETCore.App 6.0.0
Microsoft.WindowsDesktop.App 5.0.12
Microsoft.WindowsDesktop.App 6.0.0
[错误图片] https://i.stack.imgur.com/5tFrN.jpg
按照下面的代码进行
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Shop.Data;
using Shop.Models;
//Endpoint = URL
//http://localhost:5000
//https://localhost:5001
[Route("categories")]
public class CategoryController : ControllerBase
[HttpGet]
[Route("")]
public ActionResult<List<Category>> Get()
return new List<Category>();
[HttpGet]
[Route("id:int")]
public ActionResult<Category> GetById(int id)
return new Category();
[HttpPost]
[Route("")]
public async Task<ActionResult<List<Category>>> Post(
[FromBody] Category model,
[FromServices] DataContext context)
if (!ModelState.IsValid)
return BadRequest(ModelState);
context.Categories.Add(model);
await context.SaveChangesAsync();
return Ok(model);
[HttpPut]
[Route("id:int")]
public ActionResult<List<Category>> Put(int id, [FromBody] Category model)
if (id != model.Id)
return NotFound(new message = "Categoria não encontrada" );
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok(model);
[HttpDelete]
[Route("id:int")]
public ActionResult<List<Category>> Delete()
return Ok();
【问题讨论】:
【参考方案1】:对不起,我安装了 Swashbuckle.AspeNetCore.Swagger 5.0.0 并在 Startup.cs 中调用它。 我将两者都删除了,效果很好。
【讨论】:
以上是关于C# Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常的主要内容,如果未能解决你的问题,请参考以下文章