Search code examples
c#asp.net-core-mvc.net-6.0

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
    });
}

enter image description here

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 notified that OkObjectResult not working too.

.csproj config

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>

Solution

  • 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