BadRequestObjectResult(Errors.AddErrorsToModelState(result,ModelState))无法正常工作

时间:2019-01-21 07:47:54

标签: c# asp.net-core

我正在使用来自“ https://fullstackmark.com/post/13/jwt-authentication-with-aspnet-core-2-web-api-angular-5-net-core-identity-and-facebook-login”的代码,但是由于出现以下错误,所以我无法通过AccountsController:“名称'Errors'在当前上下文中不存在”

这是我的代码:

panelColor

我在此行得到错误:

  

如果(!result.Succeeded)返回新的BadRequestObjectResult(Errors.AddErrorsToModelState(result,ModelState));

enter image description here

您能告诉我如何做到这一点吗?比你好。

1 个答案:

答案 0 :(得分:1)

Errors不超过a plain custom helper class

要解决您的问题,只需添加以下类:

    public static class Errors
    {
        public static ModelStateDictionary AddErrorsToModelState(IdentityResult identityResult, ModelStateDictionary modelState)
        {
            foreach (var e in identityResult.Errors)
            {
                modelState.TryAddModelError(e.Code, e.Description);
            }

            return modelState;
        }

        public static ModelStateDictionary AddErrorToModelState(string code, string description, ModelStateDictionary modelState)
        {
            modelState.TryAddModelError(code, description);
            return modelState;
        }
    }