Search code examples
razor-pagesasp.net-core-identity

How to redirect to ASP.NET Ideneity login page?


Using ASP.NET Identity, if I want to construct an <a> element that links to the Login page I can use Razor Helpers and some magic strings:

<a asp-area="Identity" asp-page="/Account/Login">Login</a>

(Not the question at hand, but I'd be happy if someone would tell me why this convoluted collection of magic strings is somehow better than hardcoding the "/Identity/Account/Login" relative URL.)

My question is: If I want to return a redirect from a Handler routine (i.e., the C# code behind a razor page), is there some recommended magic that I should be using to get a relative URL to the Login page?

In other words, is there something better than:

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

Solution

  • Use it this way:

    return RedirectToPage("/Account/Login", new { area = "Identity" });