SyntaxError:JSON.parse:当我返回Asp Core webapi中的角色列表时

时间:2018-11-05 09:55:04

标签: asp.net-web-api asp.net-core asp.net-core-2.0 asp.net-core-webapi

我需要通过Asp CoreAngular6Identity Core 2.2Web Apiprivate readonly IApplicationRoleManager _roleManag; public RoleManagerController(IApplicationRoleManager roleManag) { _roleManag = roleManag; } [HttpGet] [Route("GetRoles")] public async Task<List<Role>> GetRoles() { var res =await _roleManag.GetAllCustomRolesAsync(); return res; } 中使用动态角色权限

当我需要返回下拉列表中显示的角色列表时,它会向我显示此错误:

  

SyntaxError:JSON.parse:JSON数据第1行第1列的数据意外结束

现在:如何解决返回角色列表的问题?

此我的代码:

RoleManagerController:

 public interface IApplicationRoleManager : IDisposable
{
    Task<List<Role>> GetAllCustomRolesAsync();
}

IApplicationRoleManager:

 public class ApplicationRoleManager : RoleManager<Role>, IApplicationRoleManager
{

    private readonly IHttpContextAccessor _contextAccessor;
    private readonly IUnitOfWork _uow;
    private readonly IdentityErrorDescriber _errors;
    private readonly ILookupNormalizer _keyNormalizer;
    private readonly ILogger<ApplicationRoleManager> _logger;
    private readonly IEnumerable<IRoleValidator<Role>> _roleValidators;
    private readonly IApplicationRoleStore _store;
    private readonly DbSet<User> _users;

    public ApplicationRoleManager(
        IApplicationRoleStore store,
        IEnumerable<IRoleValidator<Role>> roleValidators,
        ILookupNormalizer keyNormalizer,
        IdentityErrorDescriber errors,
        ILogger<ApplicationRoleManager> logger,
        IHttpContextAccessor contextAccessor,
        IUnitOfWork uow) :
        base((RoleStore<Role, ApplicationDbContexct, int, UserRole, RoleClaim>)store, roleValidators, keyNormalizer, errors, logger)
    {
        _store = store;
        _store.CheckNullArgument(nameof(_store));

        _roleValidators = roleValidators;
        _roleValidators.CheckNullArgument(nameof(_roleValidators));

        _keyNormalizer = keyNormalizer;
        _keyNormalizer.CheckNullArgument(nameof(_keyNormalizer));

        _errors = errors;
        _errors.CheckNullArgument(nameof(_errors));

        _logger = logger;
        _logger.CheckNullArgument(nameof(_logger));

        _contextAccessor = contextAccessor;
        _contextAccessor.CheckNullArgument(nameof(_contextAccessor));

        _uow = uow;
        _uow.CheckNullArgument(nameof(_uow));

        _users = _uow.Set<User>();
    }

     public async Task<List<Role>> GetAllCustomRolesAsync()
    {
        return await Roles.ToListAsync();
    }

ApplicationRoleManager:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.Configure<SiteSetting>(options => Configuration.Bind(options));
        services.AddDbContext<ApplicationDbContexct>(option =>
            option.UseSqlServer(Configuration.GetConnectionString("ApplicationConnectionString")));
        services.AddIdentity<User, Role>()
            .AddEntityFrameworkStores<ApplicationDbContexct>()
            .AddDefaultTokenProviders();
        services.AddScoped<IApplicationRoleStore, ApplicationRoleStore>();
        services.AddScoped<RoleStore<Role, ApplicationDbContexct, int, UserRole, RoleClaim>, ApplicationRoleStore>();
        services.AddScoped<IApplicationRoleManager, ApplicationRoleManager>();
        services.AddScoped<RoleManager<Role>, ApplicationRoleManager>();
        services.AddScoped<IUnitOfWork,ApplicationDbContexct>();
        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
        });
    }

修改

新错误:

Error...............................

启动:

a = [x1,x2,....xn]

1 个答案:

答案 0 :(得分:0)

为什么在2.2.0-preview3-35497文件中使用EntityFrameworkCore StoreFinal.csproj

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0-preview3-35497" />

它与您正在使用的其他库不兼容。这就是为什么您遇到MissingMethodException / method not found错误的原因。尝试在各处使用相同的版本,并且现在不使用预览版本。还可以使用https://github.com/jerriep/dotnet-outdated正确且一致地更新您的依赖项。

dotnet outdated -u