Dan Champagne
Dan Champagne

Reputation: 920

Why are my tag helpers not resolving correctly?

So I have an interesting thing happening. I'm creating a new .NET Core MVC app, and I'm adding default authentication. Now the generated _LoginPartial has the following line:

<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>

But when it's rendered on the page, the href attribute comes out like this:

<a class="nav-link text-dark" href="/?area=Identity&amp;page=%2FAccount%2FLogin">Login</a>

I'm really unsure why this is happening. This is also happening with @Url.Action when I try to add in an area. I must be missing something, but I'm absolutely lost.

EDIT:

Here is the ConfigureServices and Configure methods.

public void ConfigureServices(IServiceCollection services)
    {

        services.AddIdentity<ApplicationUser, MongoIdentityRole>()
                .AddMongoDbStores<ApplicationUser, MongoIdentityRole, Guid>("mongodb://localhost:27017", "gw")
                .AddSignInManager()
                .AddDefaultTokenProviders();

        services.AddMvc()
            .AddRazorPagesOptions(options => {})
            .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);

        services.Configure<IdentityOptions>(options => {
            // Password settings.
            options.Password.RequireDigit = true;
            options.Password.RequireLowercase = true;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = true;
            options.Password.RequiredLength = 8;
            options.Password.RequiredUniqueChars = 1;

            // Lockout settings.
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;
            options.Lockout.AllowedForNewUsers = true;

            // User settings.
            options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
            options.User.RequireUniqueEmail = false;
        });

        services.ConfigureApplicationCookie(options => {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

            options.LoginPath = "/Identity/Account/Login";
            options.AccessDeniedPath = "/Identity/Account/AccessDenied";
            options.SlidingExpiration = true;
        });



        // services.AddTransient<IEmailSender, AuthMessageSender>();
        // services.AddTransient<ISmsSender, AuthMessageSender>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();


        app.UseEndpoints(endpoints => {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

    }

Upvotes: 1

Views: 145

Answers (3)

Ryan
Ryan

Reputation: 20116

I could not reproduce your problem but based on this issue, you could try to add .AddDefaultUI() in the Startup.cs:

services.AddIdentity<ApplicationUser, MongoIdentityRole>()
            .AddMongoDbStores<ApplicationUser, MongoIdentityRole, Guid>("mongodb://localhost:27017", "gw")
            .AddSignInManager()
            .AddDefaultUI()
            .AddDefaultTokenProviders();

Upvotes: 1

LazZiya
LazZiya

Reputation: 5729

Most probably endpoint routing is not configured properly, MapRazorPages() must be configured as well:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});

Upvotes: 0

Roman.Pavelko
Roman.Pavelko

Reputation: 1675

If first answer does not help also make sure your DefaultIdentity is registered as well:

services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<ApplicationDbContext>();

Upvotes: 0

Related Questions