[SOLVED] How to make the RedirectToPage work When Using Areas

Issue

I am working on a .NET 6 Web app for testing, and run into problem with RedirectToPage in a Razor page when using Areas. Here are the details.

TEST ENVIRONMENT

ASP.NET framework: .NET 6
App pattern: Razor pages (without MVC)
Authentication: with simple cookies
IDE: Visual Studio 2022
Developer machine: Windows 11

PROJECT STRUCTURE

MyProject
    Areas
        |----Public
                |----Pages
                ......
                
        |----Internal
                |----Pages
                        |---- Index.cshtml
                        |---- Authenticate
                                |---- UserLogin.cshtml
                                      UserLogin.cs
                        |---- Products
                        |---- Services
                           
    Pages
        |---- Shared
        |---- Index.cshtml
        |---- Error.cshtml

PROBLEM DESCRIPTION

After a user provides the credential at the UserLogin.cshtml page, the "OnPost" method in UserLogin.cs class handles the verification. If the login is successful, it should redirect the user to the Index.cshtml page in the root of Internal area (see the code segment below).

I have tried different ways to specify the page name in RedirectToPage statement. But, none of them worked. (Note: Using browser, I can access the index page using url"http://localhost:12345/Internal/Index")

"Index"
"/Index"
"./Index"
"/Internal/Index" (without using 2nd input param)
"./Internal/Index" (without using 2nd input param)
public async Task<IActionResult> OnPost()
{
    // verify user ID and password against DB
    bool IsVerified = CheckUserLogin(LoginUser.UserId, LoginUser.Password);

    if (IsVerified)
    {
        var claims = new List<Claim>();
        claims.Add(new Claim("username", LoginUser.UserId));
        claims.Add(new Claim(ClaimTypes.NameIdentifier, LoginUser.UserId));

        // test statement for using name property in claim
        claims.Add(new Claim(ClaimTypes.Name, LoginUser.FullName));

        //test statements for using user role property in clain
        claims.Add(new Claim(ClaimTypes.Role, "admin,research"));

        var claimIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
        var claimPrinciple = new ClaimsPrincipal(claimIdentity);
        await HttpContext.SignInAsync(claimPrinciple);

        return RedirectToPage("Index", new { area = "Internal" });
    }
    return Page();
}

QUESTION
What is the correct way of specifying the "route" in RedirectToPage statement for a razor page under an Area?

Solution

According to your structure,I suggest you with two options:

Option1: If you use Razor View, try return RedirectToPage("../Index");

Option2: If you use Razor Page, try return RedirectToPage("/Index", new { area = "Internal" });

Answered By – Qing Guo

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *