【问题标题】:View isn't loading asp.net core mvc查看没有加载 asp.net core mvc
【发布时间】:2021-01-06 20:16:21
【问题描述】:

我第一次在 asp.net core 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();
        }

授权正常工作。通过我的 Login 方法登录后,我可以通过自己输入链接来访问 [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}");
            });

        }
    }

【问题讨论】:

  • 所以我假设当您登录时它只会让您留在登录页面上?
  • @janzen 是的,它只是让我留在登录页面
  • Startup.cs 的 Configure 方法中是否设置了端点或 maproute?如果是这样,请将其添加到问题中。
  • @janzen Okey 我添加了它
  • 我建议,如果您想重定向到“/Teacher/Panel/Index”,那么可以将其添加到 MapRoute 部分。此外,您在面板/索引上有一个断点,也在登录方法中添加一个断点。并且更详细地描述当 Panel/Index 断点被命中时会发生什么。是否有错误,是否返回视图?

标签: c# asp.net-core-mvc authorization


【解决方案1】:

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

在前面的代码中,exists 应用了路由必须匹配区域的约束。

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

另外,你可以参考这个document

【讨论】:

  • 感谢您的回答,但不幸的是,这并没有解决我的问题 :(
  • @yigit.gok,好的,不客气,我不知道你的ajax。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 2021-05-22
    • 2020-03-12
    • 2019-09-07
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多