Home > Enterprise >  How to Redirect Controller to razor page in ASP.NET Core
How to Redirect Controller to razor page in ASP.NET Core

Time:11-08

I send data from Index.cshtml to my controller. Then I want to redirect from the controller to the index.cshtml page inside the Admin folder (those two are two different index.cstml files). I added a screenshot of the file hierarchy. Please help me to solve this.

I tried this, but it's not working

public IActionResult Login([FromBody] Users user)
{
    if (user.UserName.Equals("admin") & user.Password.Equals("admin"))
    {
        return LocalRedirect("/Admin/Index"); // redirect to index page in Admin folder
    }
    else
        return RedirectToPage("Error");
}

Folder structure:

folder structure for better understand

CodePudding user response:

How about this?

if (user.UserName.Equals("admin") & user.Password.Equals("admin"))
{
    // redirect to index page in Admin folder
    return RedirectToPage("Admin/Index"); 
}
....

CodePudding user response:

If you want to go to an action, you would pass the action as the first argument and the controller as the second argument.

RedirectToAction("Index", "Admin").

If you just want to redirect to the page itself, you would go to the directory that the view is located. Such as return View("~/Views/Chats/Index.cshtml");

return View("~/Views/Admin/Index.cshtml");

In the code above you would replace the argument with wherever your page is located.

  • Related