如何从 Asp.Net Core MVC 控制器重定向到 Razor 页面编写的登录页面

问题描述 投票:0回答:1

我是 Asp.Net Core MVC 的新手。我在.Net Core 6.0 中创建了一个 MVC 应用程序。当我构建身份时,它在 Razor 页面中创建它。用户输入登录凭据后,Login Razor 页面会检查用户是否需要提供任何其他信息。如果是,它会重定向到 MVC 控制器。将此信息保存在数据库中后,我必须将用户重定向回登录页面。

[HttpPost]
public IActionResult SecretQuestionSetup(SecretQuestionVM secretQuestionVM)
{
   if (ModelState.IsValid)
   {
      //code to save data in the db
     
   }

   return RedirectToAction("Identity/Account/Login");
 
}

它正在尝试重定向到:https://localhost:7281/MyController/Identity%2FAccount%2FLogin 正如您所看到的,它包含控制器名称“MyController”作为 URL 的一部分。如果重定向 URL 是 https://localhost:7281/Identity/Account/Login,那么它就会起作用。我尝试在 Program.cs 中添加以下代码,但仍然没有解决问题。

builder.Services.ConfigureApplicationCookie(options =>
{
   options.LoginPath = "/login";
});
asp.net-mvc asp.net-core asp.net-identity
1个回答
0
投票

在 MVC 控制器中,如果您想重定向到 Razor 页面,则不会选择 RedirectToAction,因为它将结果返回到指定的控制器和操作方法 https://dotnettutorials.net/lesson/redirect-redirecttoaction-mvc/

Program.cs 中的配置不是必需的。你需要改变方法。

[HttpPost]
public RedirectResult SecretQuestionSetup(SecretQuestionVM secretQuestionVM)
{
   if (ModelState.IsValid)
   {
      //code to save data in the db
     
   }

   return Redirect("/Identity/Account/Login");
 
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.