【问题标题】:Getting "Inconsistent accessibility parameter type" error in blazor webassembly在 blazor webassembly 中出现“不一致的可访问性参数类型”错误
【发布时间】:2021-08-17 14:26:43
【问题描述】:

您好,只是想澄清一下

我正在为 Blazor Web 程序集开发一个项目,我的 Api 控制器给出以下错误消息。

错误 CS0051 可访问性不一致:参数类型“IAuthRepository”的可访问性低于方法“AuthController.AuthController(IAuthRepository)”BattlesBlazor.Server

这是我的服务响应类

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 上没有响应

如果有人可以请给我一些建议,我将不胜感激

【问题讨论】:

    标签: c# asp.net-mvc blazor-webassembly asp.net5


    【解决方案1】:

    将控制器方法的返回类型更改为Task&lt;IActionResult&gt;

    [HttpPost("register")]
    public async Task<IActionResult> Register(UserRegister request)
    ...
    

    或者将IAuthRepository设为公开并始终返回response

    【讨论】:

      【解决方案2】:

      这里代码中的返回类型是问题:

      [HttpPost("register")]
      public async Task<IAuthRepository> Register(UserRegister request)
      

      AuthControllerpublicRegister 也是,所以 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 interface IAuthRepository)。另一方面,Register 返回IAuthRepository 似乎很奇怪。它不应该返回ServiceResponse&lt;int&gt;吗?:

      [HttpPost("register")]
      public async Task<ServiceResponse<int>> Register(UserRegister request)
      

      【讨论】:

      • 错误 CS0266 无法将类型“Microsoft.AspNetCore.Mvc.BadRequestObjectResult”隐式转换为“BattlesBlazor.Server.Data.IAuthRepository”。存在显式转换(您是否缺少演员表?) BattlesBlazor.Server --- 现在这是我遇到的错误
      • 看起来是个例外,非常清楚。
      • @DumbPerson 是的,您似乎不太可能想要返回 IAuthRepository,正如我上面提到的那样。
      • 非常感谢,我真的应该多加注意,非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      相关资源
      最近更新 更多