所以,我这几天一直在观看和学习.net core。我已经构建了功能API(使用swagger)有一个我现在使用的控制器,它与我的问题相对应(怀疑它有问题,但要完整):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BrambiShop.API.Data;
using BrambiShop.API.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace BrambiShop.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CategoriesController : ControllerBase
{
private BrambiContext _context;
public CategoriesController(BrambiContext context)
{
_context = context;
}
// GET: api/ItemVariants
[HttpGet]
public async Task<IEnumerable<Category>> GetAsync()
{
return await _context.Categories.ToListAsync();
}
// GET: api/ItemVariants/5
[HttpGet("{id}")]
public async Task<Category> GetAsync(int id)
{
return await _context.Categories.FindAsync(id);
}
// POST-add: api/ItemVariants
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] Category item)
{ …Run Code Online (Sandbox Code Playgroud)