自动重定向到 public 视图

Automatically redirect to public view

我的 ASP.NET 核心应用程序需要用户登录。我创建了一个允许匿名用户的 public 操作方法,即

[AllowAnonymous]
public IActionResult Public()
{
   return View();
}

因为在我的 Startup.cs 中我需要身份验证,用户会自动重定向到登录页面。

我想更改行为并自动将用户重定向到 public 页面,让他们单击 link 登录。

如何将我的用户重定向到 public 页面而不是登录页面?

只需更改 App_Start 中的默认映射:

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "Public", action = "Public", 
        id = UrlParameter.Optional }
);

更新

在您发表评论后,我认为这将满足您的需求:

services.Configure<IdentityOptions>(options =>
{
    options.Cookies.ApplicationCookie.LoginPath = new PathString("/Home/Public");
});

将其添加到Startup.cs

中的ConfigureServices方法中