【问题标题】:The type or namespace name 'Application' does not exist in the namespace 'WebApplication1.Models'命名空间“WebApplication1.Models”中不存在类型或命名空间名称“Application”
【发布时间】:2020-08-09 15:22:06
【问题描述】:

您好,我是 Net Core 的新手,我正在关注 PragimTech 的 this 教程。在扩展教程的 IdentityUser Part(77) 时,当我为 ApplicationUser 创建新类以便从 IdentityUser 扩展时,我面临编译错误提示

命名空间中不存在类型应用程序

。有我的代码要显示。

ApplicationUser.cs

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Models
{
    public class ApplicationUser:IdentityUser
    {

    }
}

_ViewImports.cshtml

@using WebApplication1.Models
@using WebApplication1.ViewModels
@using Microsoft.AspNetCore.Identity

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Startup.cs 文件中的 ConfigureServices 方法

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContextPool<AppDbContext>(
            options => options.UseSqlServer(_config.GetConnectionString("EmployeeDBConnection"))
          );
        services.AddMvc(config => {
            var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        });
        services.AddScoped< IEmployeeRepository, SqlEmployeeRepository> ();
        services.AddIdentity<ApplicationUser, IdentityRole>(options => {
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequiredLength = 4;
        }).
            AddEntityFrameworkStores<AppDbContext>();
    }

_layout.cshtml中的注入部分

@inject SignInManager<ApplicationUser> signeInManager;

帐户控制器

public class AccountController : Controller
    {
        private readonly UserManager<ApplicationUser> userManager;
        private readonly SignInManager<ApplicationUser> signInManager;

        public AccountController(UserManager<ApplicationUser> userManager, 
                            SignInManager<ApplicationUser> signInManager)
        {
            this.userManager = userManager;
            this.signInManager = signInManager;
        }


        [AcceptVerbs("Post","Get")]
        [AllowAnonymous]
        public async Task< IActionResult> IsEmailInUse(string email)
        {
            var user = await userManager.FindByEmailAsync(email);

            if(user == null)
            {
                 return Json(true);
            }
            else
            {
                return Json($"Email {email} is already taken.");
              }
         }

         [HttpGet]
         [AllowAnonymous]
         public IActionResult Register()
         {
             return View();
          }

         [HttpPost]
         [AllowAnonymous]
         public async Task< IActionResult> Register(RegisterViewModel model)
         {
             if (ModelState.IsValid)
              {
                 var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                 var result = await userManager.CreateAsync(user, model.Password);
                 if (result.Succeeded)
                 {
                     await signInManager.SignInAsync(user, false);
                     return RedirectToAction("index","home");
                      }
                  else
                {
                     foreach (var error in result.Errors)
                     {
                         ModelState.AddModelError("", error.Description);
                     }
            }
        }
        return View(model);
         }

         [HttpGet]
         [AllowAnonymous]
         public IActionResult Login()
         {
             return View();
          }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var result = await signInManager.PasswordSignInAsync(model.Email, model.Password, 
               model.RememberMe, false);
              if (result.Succeeded)
              {
                   if(!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                   {
                    return Redirect(returnUrl);
                    }else 
                     return RedirectToAction("index", "home");
                 }
                 else
                 {
                     ModelState.AddModelError(string.Empty, "Invalid user or password");
                  }
              }
               return View(model);
         }

          [HttpPost]
          public async Task<IActionResult> Logout()
          {
              await signInManager.SignOutAsync();
              return RedirectToAction("Index", "home");
           }

             }

【问题讨论】:

  • 根据您的代码,我没有发现您使用过应用程序类型或命名空间。你能告诉我你在哪一行抛出了这个错误吗?
  • 我在 _viewImport.cshtml 中导入了命名空间;如您所见,@using Webapplication.Models 用于 Models 命名空间,而我的 ApplicationUser 类位于 Models 文件夹中。我什至在注入标签中使用了 WebApplication.Models.ApplicationUser ,但发生了同样的错误。错误在 _layout.cshmtl 内的 SignInManager 内
  • 可以分享一下_layout.cshmtl里面SignInManager相关的代码吗?

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


【解决方案1】:

刚刚清理了解决方案并重新构建了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2022-01-11
    相关资源
    最近更新 更多