【问题标题】:Why are my tag helpers not resolving correctly?为什么我的标签助手不能正确解析?
【发布时间】:2020-01-24 02:33:46
【问题描述】:

所以我发生了一件有趣的事情。我正在创建一个新的 .NET Core MVC 应用程序,并且正在添加默认身份验证。现在生成的 _LoginPartial 有以下行:

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

但是当它在页面上渲染时,href 属性是这样出来的:

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

我真的不确定为什么会这样。当我尝试添加一个区域时,@Url.Action 也会发生这种情况。我一定是错过了什么,但我完全迷失了。

编辑:

这里是 ConfigureServices 和 Configure 方法。

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

    }

【问题讨论】:

    标签: c# authentication asp.net-core


    【解决方案1】:

    我无法重现您的问题,但基于此issue,您可以尝试在Startup.cs 中添加.AddDefaultUI()

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

    【讨论】:

    • 我不知道为什么或如何,但添加“AddDefaultUI()”纠正了整个应用程序的标签助手问题。非常感谢!
    【解决方案2】:

    如果第一个答案没有帮助,还请确保您的 DefaultIdentity 也已注册:

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

    【讨论】:

    • 可悲的是,这似乎没有帮助!
    【解决方案3】:

    很可能端点路由配置不正确,MapRazorPages() 也必须配置:

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

    【讨论】:

    • 我试过了,但对链接问题没有帮助。如果有帮助,我添加了配置方法。
    • 而不是services.AddMvc() 尝试使用services.AddControllersWithViews()services.AddRazorPages()
    • 另一种解决方案,use mvc without endpoint routing:services.AddMvc(options =&gt; options.EnableEndpointRouting = false);
    • 这两个选项都没有,但似乎并没有解决问题。我还添加了对 Microsoft.AspNetCore.Mvc.TagHelpers 的引用,看看它是否会起作用,但它仍然不起作用。
    猜你喜欢
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多