在 blazor webassembly 中出现 "Inconsistent accessibility parameter type" 错误
Getting "Inconsistent accessibility parameter type" error in blazor webassembly
你好只是想得到一些澄清
我正在为 Blazor Web 程序集开发一个项目,我的 Api 控制器给出了以下错误消息。
Error CS0051 Inconsistent accessibility: parameter type 'IAuthRepository' is less accessible than method 'AuthController.AuthController(IAuthRepository)' BattlesBlazor.Server
这是我的服务响应class
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BattlesBlazor.Shared
{
public class ServiceResponse<T>
{
public T Data { get; set; }
public bool Success { get; set; }
public string Message { get; set; }
}
}
这是我的 IAuthRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BattlesBlazor.Server.Data
{
interface IAuthRepository
{
Task<ServiceResponse<int>> Register(User user, string password);
Task<ServiceResponse<string>> Login(string email, string password);
Task<bool> UserExists(string email);
}
}
这是 AuthController
using BattlesBlazor.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BattlesBlazor.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IAuthRepository _authRepo;
public AuthController(IAuthRepository authRepo)
{
_authRepo = authRepo;
}
[HttpPost("register")]
public async Task<IAuthRepository> Register(UserRegister request)
{
var response = await _authRepo.Register(
new User {
Username = request.Username,
Email = request.Email,
Bananas = request.Bananas,
DateOfBirth =request.DateOfBirth,
IsConfirmed = request.IsConfirmed
}, request.Password );
if (!response.Success)
{
return BadRequest(response);
}
return Ok(response);
}
}
我尝试从“public class AuthController : ControllerBase”中删除“public”,这确实消除了错误,但是在 PostMan 上没有响应
如果有人能给我一些建议,我将不胜感激
此处代码中的 return 类型是问题所在:
[HttpPost("register")]
public async Task<IAuthRepository> Register(UserRegister request)
AuthController
是public
,Register
也是,所以IAuthRepository
也一定是public
.
从您的定义中,我们可以看出 IAuthRepository
是默认值 (internal
):
interface IAuthRepository
{
Task<ServiceResponse<int>> Register(User user, string password);
Task<ServiceResponse<string>> Login(string email, string password);
Task<bool> UserExists(string email);
}
一般来说,解决办法就是让这些一致。那就是:你应该IAuthRepository
public(即public interface IAuthRepository
)。另一方面,Register
returns IAuthRepository
似乎很奇怪。不应该是returnServiceResponse<int>
吗?:
[HttpPost("register")]
public async Task<ServiceResponse<int>> Register(UserRegister request)
将控制器方法的 return 类型更改为 Task<IActionResult>
:
[HttpPost("register")]
public async Task<IActionResult> Register(UserRegister request)
...
或使 IAuthRepository
public 并始终 return response
。
你好只是想得到一些澄清
我正在为 Blazor Web 程序集开发一个项目,我的 Api 控制器给出了以下错误消息。
Error CS0051 Inconsistent accessibility: parameter type 'IAuthRepository' is less accessible than method 'AuthController.AuthController(IAuthRepository)' BattlesBlazor.Server
这是我的服务响应class
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BattlesBlazor.Shared
{
public class ServiceResponse<T>
{
public T Data { get; set; }
public bool Success { get; set; }
public string Message { get; set; }
}
}
这是我的 IAuthRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BattlesBlazor.Server.Data
{
interface IAuthRepository
{
Task<ServiceResponse<int>> Register(User user, string password);
Task<ServiceResponse<string>> Login(string email, string password);
Task<bool> UserExists(string email);
}
}
这是 AuthController
using BattlesBlazor.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BattlesBlazor.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IAuthRepository _authRepo;
public AuthController(IAuthRepository authRepo)
{
_authRepo = authRepo;
}
[HttpPost("register")]
public async Task<IAuthRepository> Register(UserRegister request)
{
var response = await _authRepo.Register(
new User {
Username = request.Username,
Email = request.Email,
Bananas = request.Bananas,
DateOfBirth =request.DateOfBirth,
IsConfirmed = request.IsConfirmed
}, request.Password );
if (!response.Success)
{
return BadRequest(response);
}
return Ok(response);
}
}
我尝试从“public class AuthController : ControllerBase”中删除“public”,这确实消除了错误,但是在 PostMan 上没有响应
如果有人能给我一些建议,我将不胜感激
此处代码中的 return 类型是问题所在:
[HttpPost("register")]
public async Task<IAuthRepository> Register(UserRegister request)
AuthController
是public
,Register
也是,所以IAuthRepository
也一定是public
.
从您的定义中,我们可以看出 IAuthRepository
是默认值 (internal
):
interface IAuthRepository
{
Task<ServiceResponse<int>> Register(User user, string password);
Task<ServiceResponse<string>> Login(string email, string password);
Task<bool> UserExists(string email);
}
一般来说,解决办法就是让这些一致。那就是:你应该IAuthRepository
public(即public interface IAuthRepository
)。另一方面,Register
returns IAuthRepository
似乎很奇怪。不应该是returnServiceResponse<int>
吗?:
[HttpPost("register")]
public async Task<ServiceResponse<int>> Register(UserRegister request)
将控制器方法的 return 类型更改为 Task<IActionResult>
:
[HttpPost("register")]
public async Task<IActionResult> Register(UserRegister request)
...
或使 IAuthRepository
public 并始终 return response
。