The type or namespace name 'Application' does not exist in the namespace 'WebApplication1.Models'

All we need is an easy explanation of the problem, so here it is.

Hi I am new to Net Core and I am following this tutorial from PragimTech. In extending IdentityUser Part(77) of tutorial when I create new Class for ApplicationUser in order to extend from IdentityUser I face a Compile error saying

type Application does not exist in namespace

. There are my codes to show.

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

The ConfigureServices method in Startup.cs file

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

The injection part in _layout.cshtml

@inject SignInManager<ApplicationUser> signeInManager;

AccountController

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

             }

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Just Cleaned the solution and re-built it.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply