英文:
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( "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 notified that OkObjectResult not working too.
.csproj config
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论