【问题标题】:SyntaxError: JSON.parse: when i return List of roles in Asp Core webapiSyntaxError: JSON.parse: 当我在 Asp Core webapi 中返回角色列表时
【发布时间】:2019-04-08 16:04:35
【问题描述】:

我需要在Asp CoreAngular6 中使用Identity Core 2.2 by Web Api 中的动态角色权限

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

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

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

这是我的代码:

角色管理器控制器:

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

IApplicationRoleManager:

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

应用角色管理器:

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

编辑

新错误:

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

启动:

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

【问题讨论】:

  • 嗯,json 长什么样子?
  • @PeterBons 它在 mozila 中向我展示了这个:SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
  • 如果你从网络浏览器或邮递员访问GetRoles,你会得到预期的json吗?检查网络浏览器网络选项卡以查看来自角度的请求时的真实响应。对于异步方法,您需要调用 await 来获取响应,这意味着更改 public async Task&lt;List&lt;Role&gt;&gt; GetRoles() { var res = await _roleManag.GetAllCustomRolesAsync(); return res; }
  • @TaoZhou 现在它告诉我这个错误:MissingMethodException: Method not found: 'System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_SelectAsyncMethod()'.
  • 查询 dbcontext 时,您的代码有问题。你从哪里得到这个错误?检查您是否得到对象var res 的预期结果。另外,检查return Roles.ToListAsync();的结果

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


【解决方案1】:

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

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

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

dotnet outdated -u

【讨论】:

    猜你喜欢
    • 2019-07-11
    • 2019-12-27
    • 2021-10-10
    • 2014-02-27
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多