【发布时间】: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();
}
【问题讨论】:
标签: c# asp.net visual-studio-code postman dotnet-cli