视图未加载 asp.net 核心 mvc

View isn't loading asp.net core mvc

我第一次在 asp.net 核心 mvc 中使用 [Authorize] 属性。但是当用户正确登录时,我无法重定向到我的索引页面。 我可以触发“面板”控制器的“索引”操作,但它的视图未加载。断点到这里没有任何反应:

**(Teacher Area PanelController)**
public IActionResult Index()
        {
            return View();
        }

这是我的登录方法:

**(AccountController)**
[HttpPost]
        public async Task<IActionResult> Login(LoginRequest loginRequest)
        {
            if (_loginService.Login(loginRequest))
            {
                var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, loginRequest.Mail)
            };
                var userIdentity = new ClaimsIdentity(claims, "Login");
                ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
                await HttpContext.SignInAsync(principal);
                return RedirectToAction("Index","Panel",new { area="Teacher" });
            }
            return View();
        }

授权工作正常。通过登录方法登录后,我可以通过自己输入 link 来访问 [Authorize] 属性下的方法。我多次搜索解决方案,但我一无所获。

谢谢...

编辑: 这是我的 ConfigureServices 方法:

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(opt => opt.EnableEndpointRouting = false);
            services.AddRazorPages();
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options =>
        {
            options.LoginPath = "/Account/Login";
        });
            services.AddSingleton<IFileProvider>(
            new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")));
            services.AddControllersWithViews();
            services.AddSingleton<IUserService, UserService>();
            services.AddSingleton<ILoginService, LoginService>();
            services.AddSingleton<IUserRepository, UserRepository>();
        }

和配置方法:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseAuthentication();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            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.UseAuthorization();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "areas",
                    template: "{area:exists}/{controller=Home}/{action=Index}");
                routes.MapRoute(
                   name: "default",
                   template: "{controller=Home}/{action=Index}");
            });

        }
    }

您可以尝试删除 MapRoute 中的 exists。这是官方描述:

在上述代码中,存在应用路由必须匹配区域的约束。

RedirectToAction会根据路由模板生成路由

另外,可以参考这个document

问题已解决。我用 ajax 发送了登录信息。当我删除 ajax 并只发送表单操作时,我的问题就解决了。感谢您的回答。