ASP.NET Core 6:无法解析符号 ‘Ok’

huangapple go评论56阅读模式
英文:

ASP.NET Core 6 : cannot resolve symbol 'Ok'

问题

I have some issues with ASP.NET Core 6.

I have a method in a controller:

[HttpPost("create")]
public async Task<IActionResult> CreateAccountAsync()
{
    return Ok(new ApiResponse<object>
    {
        IsSuccess = true
    });
}

I was doing it in .NET Framework and it works fine.

This is also full controller code:

using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using MobileTrackerWebApi.ApiHandlers;
using MobileTrackerWebApi.Database;
using MobileTrackerWebApi.System;

namespace MobileTrackerWebApi.Controllers;

[Controller]
[Route("account")]
public class AccountController
{
    private static ApiDbContext _dbContext;
    private static EnvReader _reader;
    
    public AccountController(EnvReader envReader, ApiDbContext dbContext)
    {
        Guard.NotNull(envReader);
        Guard.NotNull(dbContext, ErrorCode.DbContextIsNull);

        _reader = envReader;
        _dbContext = dbContext;
    }

    [HttpPost("create")]
    public async Task<IActionResult> CreateAccountAsync()
    {
        return Ok(new ApiResponse<object>
        {
            IsSuccess = true
        });
    }
}

I also noticed that OkObjectResult is not working too.

.csproj config

<PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
英文:

I have some issues with ASP.NET Core 6.

I have a method in a controller:

[ HttpPost( &quot;create&quot; ) ]
public async Task&lt;IActionResult&gt; CreateAccountAsync()
{
    return Ok( new ApiResponse&lt;object&gt;
    {
        IsSuccess = true
    });
}

ASP.NET Core 6:无法解析符号 ‘Ok’

I was doing it in .NET Framework and it works fine.

This is also full controller code:

using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using MobileTrackerWebApi.ApiHandlers;
using MobileTrackerWebApi.Database;
using MobileTrackerWebApi.System;

namespace MobileTrackerWebApi.Controllers;

[Controller]
[Route(&quot;account&quot;)]
public class AccountController
{
    private static ApiDbContext _dbContext;
    private static EnvReader    _reader;
    
    public AccountController( EnvReader envReader, ApiDbContext  dbContext)
    {
        Guard.NotNull(envReader);
        Guard.NotNull(dbContext, ErrorCode.DbContextIsNull);

        _reader = envReader;
        _dbContext = dbContext;
    }

    [ HttpPost( &quot;create&quot; ) ]
    public async Task&lt;IActionResult&gt; CreateAccountAsync()
    {
        return Ok( new ApiResponse&lt;object&gt;
        {
            IsSuccess = true
        });
    }
}

I also notified that OkObjectResult not working too.

.csproj config

    &lt;PropertyGroup&gt;
        &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
        &lt;Nullable&gt;enable&lt;/Nullable&gt;
        &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
    &lt;/PropertyGroup&gt;

答案1

得分: 3

Your controller must inherit from ControllerBase. Ok is a method of ControllerBase:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase?view=aspnetcore-7.0

英文:

Your controller must inherit from ControllerBase. Ok is a method of ControllerBase :
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase?view=aspnetcore-7.0

huangapple
  • 本文由 发表于 2023年5月7日 05:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定