无法将类型 'System.Collections.Generic.IEnumerable' 隐式转换为 'System.Web.Mvc.ActionResult'

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Web.Mvc.ActionResult'

我正在尝试 ASP.NET MVC5 Identity 并尝试实施基于声明的身份验证。

我收到以下错误:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<<anonymous type: string subject, string type, string value>>' to 'System.Web.Mvc.ActionResult'. An explicit conversion exists (are you missing a cast?)

这是一段代码:

public ActionResult GetClaims()
{
    var identity = User.Identity as ClaimsIdentity;
    var claims = from c in identity.Claims
                 select new
                 {
                     subject = c.Subject.Name,
                     type = c.Type,
                     value = c.Value
                 };
    return claims;
}

我正在关注 http://bitoftech.net/2015/03/31/asp-net-web-api-claims-authorization-with-asp-net-identity-2-1/

中的示例

几件事。您正在尝试 return 可枚举的匿名类型作为 ActionResult。通常,ActionResults 希望您 return 对传入模型的视图(razor 模板)的引用:

return View(model);

如果你只想return数据,那么你需要return一个JsonResult

return Json(new { Data = model }, JsonRequestBehavior.AllowGet);

如果它在 MVC 控制器中,您应该 return 接受 IEnumerable<Claim> 作为模型的视图:

public ActionResult GetClaims()
{
    var identity = User.Identity as ClaimsIdentity;
    var claims = from c in identity.Claims
                 select new
                 {
                     subject = c.Subject.Name,
                     type = c.Type,
                     value = c.Value
                 };
    return View(claims);
}

如果它在 api 控制器中,您可以 return IHttpActionResult

public IHttpActionResult GetClaims()
{
    var identity = User.Identity as ClaimsIdentity;
    var claims = from c in identity.Claims
                 select new
                 {
                     subject = c.Subject.Name,
                     type = c.Type,
                     value = c.Value
                 };
    return Ok(claims);
}