【问题标题】:The type or namespace name 'Application User' could not be found找不到类型或命名空间名称“应用程序用户”
【发布时间】:2016-03-25 20:29:30
【问题描述】:

我是 C# 新手,目前正在创建一个 ASP.NET MVC 应用程序。我目前在尝试按照此处找到的说明操作时遇到上述错误:Extending ASP.NET Identity

我试图让我的代码在用户登录后显示他们的名字而不是他们的电子邮件地址。

这是我的 _LoginPartial.cshtml 代码

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @{
                var manager = new UserManager<ApplicationUser>(new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(new ApplicationDbContext()));
                var currentUser = manager.FindById(User.Identity.GetUserId());
            }
            @Html.ActionLink("Hello " + currentUser.FName + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

这是我在 IdentityModels.cs 中找到的 ApplicationUser 类

public class ApplicationUser : IdentityUser
{
    public string FName { get; set; }
    public string LName { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

任何建议将不胜感激

【问题讨论】:

  • 您需要在视图中为您的 ApplicationUser 类设置 using 指令,否则该类在视图中不可见
  • 语法是什么?我试过了:@model ContactManager.Models.ApplicationUser 但没用
  • 您的视图中已经有一个“正在使用”。语法相同,只是命名空间和类不同

标签: c# asp.net asp.net-mvc


【解决方案1】:

我通过将以下内容放入 _LoginPartial.cshtml 中使其工作:

@using projectname.Models

另一个建议导致了不同的错误消息。 感谢您的帮助!

【讨论】:

    【解决方案2】:

    在您的视图中添加@using ContactManager.Models.ApplicationUser

    【讨论】:

      猜你喜欢
      • 2022-06-17
      • 1970-01-01
      • 2017-07-12
      • 2011-05-13
      • 2013-03-25
      • 2017-04-18
      • 2012-06-19
      • 2017-11-29
      相关资源
      最近更新 更多