Search code examples
c#asp.net-coreasp.net-identity

Controllers are not being hit after adding identity to project


In previous version of post I think I didn't explain the problem very well, so I'll try again from the beginning.

  1. I work on simple web app. I tried to add identity to it with scaffold option Setting of add identity operation

  2. After that I tried to run my application. It didn't work. I got System.AggregateException with message saying that my UnitOfWork class cannot be constructed. It also included two inner exception saying that program failed while validating IUnitOfWork service and second that is unable to resolve ApplicationDbContext. Whole exception message is here: exceptions messages

  3. I spotted that I got a warning message in program.cs in AddDBContext and AddDefaultIdentity methods. warnigs

  4. It turned out that add operation created additional dbContext with the same namespace as the one I provided in settings. New dbContext was created under Areas/Identity/Data/ApplicationDbContext.cs

Body of dbContext I provided in add operation:

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace BulkyBook.DataAccess;

public class ApplicationDbContext : IdentityDbContext
{
     public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
     {}

     public DbSet<Category> Categories { get; set; }
     public DbSet<CoverType> CoverTypes { get; set; }
     public DbSet<Product> Products { get; set; }
}

Body of dbContext that was created by identity:

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace BulkyBook.DataAccess;

public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
     public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
     {}

     protected override void OnModelCreating(ModelBuilder builder)
     {
         base.OnModelCreating(builder);
         // Customize the ASP.NET Identity model and override the defaults if needed.
         // For example, you can rename the ASP.NET Identity table names and more.
         // Add your customizations after calling base.OnModelCreating(builder);
     }
}
  1. Firstly I tried to change namespace of newly created dbContext. It solved warnings and application started to compile, but after compilation it does not hit home controller instead displaying blank browser page

  2. Then I tried to delete new dbContext, but results were exactly the same.

  3. Then I tried to delete all files and add identity once again, but it turned me to step 3 once again.

Below I am pasting my program.cs file and github repo link if you want to see other files. program.cs https://github.com/WojciechLarecki/BulkyBook

Thank you for your help in advance.

P.S. Application is based on MVC framework and .net 6 platform. P.S.2 The DbContext provided to scaffold option is in different project (code library) then the project i am tring to add identity to (mvc project). Whole structure of project is visible here or in repository


Solution

  • Firstly I tried to change namespace of newly created dbContext. It solved warnings and application started to compile, but after compilation it does not hit home controller instead displaying blank browser page

    Controllers are not being hit , displaying blank browser page in my test is because of 404 error.

    Try to add the [Area] attribute to associate the controller with the area,like:

    [Area("Customer")]
    public class HomeController : Controller
    {}
    

    And other controllers in the Category folder are the same, to add [Area] attribute as well . You can read Areas for controllers with views to know more.

    Besides, try to add below code to configure endpoint routing for Razor Pages , show the identity razor pages

    builder.Services.AddRazorPages();
    ...
    app.MapRazorPages();
    

    You can read Program.cs of Razor Pages to know more.