【问题标题】:Return code error in .NET 3.0 code while all libraries are included包含所有库时 .NET 3.0 代码中的返回代码错误
【发布时间】:2020-06-16 05:27:17
【问题描述】:

以下代码导致 IDE 在返回行中显示错误。这段代码是:csharp(CS0103) El nombre 'StatusCode' 不存在 en el contexto 实际 [DatingApp.API]csharp(CS0103) 当前上下文中不存在名称“StatusCode”

我正在使用 .NET 3.0,BadRequest 和 StatusCode 方法似乎是正确的,并且是 API 的一部分。

有什么可能出错的线索吗?

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Core;
using DatingApp.API.Data;
using System.Threading.Tasks;
using DatingApp.API.Models;

namespace DatingApp.API.Controllers
{
    [Route("/api/[Controller]")]
    [ApiController]
    public class AuthController
    {
        private readonly IAuthRepository _repo;

        public AuthController(IAuthRepository repo)
        {
            _repo = repo;
        }

        [HttpPost("register")]
        public async Task<IActionResult> Register(string username, string password)
        {
            username = username.ToLower();

            if (await _repo.UserExists(username)){
                return BadRequest("Nombre de usuario ya existente");
            }
            var userToCreate = new User
            {
                Username = username
            };     

            var createdUser = await _repo.Register(userToCreate, password);

            return StatusCode(201);
        }
    }
}

【问题讨论】:

标签: .net xcode return


【解决方案1】:

您需要从 ControllerBase 继承才能受益。 StatusCodeResult 和 OkObjectResult 驻留在 ControllerBase 中。希望这会有所帮助。

namespace DatingApp.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class AuthController : ControllerBase
    {
        private readonly IAuthRepository _AuthRepository;

【讨论】:

    【解决方案2】:

    AuthController 需要从 Controller 继承才能使这些方法可用。

    【讨论】:

    • 谢谢!一个例子将是非常有价值的,确实
    猜你喜欢
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多