繁体   English   中英

C# Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常

[英]C# Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request

我在这段代码中需要帮助,有些错误是不对的,哈哈……我正在尝试在 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();
    }

对不起,我安装了 Swashbuckle.AspeNetCore.Swagger 5.0.0 并在 Startup.cs 中调用它。 我删除了两者,它工作得很好。

为什么你没有在你的删除方法中定义一个参数来获取应该通过路由传递的 {id:int} 的错误

[HttpDelete]
[Route("{id:int}")]
public ActionResult<List<Category>> Delete(int id)
{
    return Ok();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM