简体   繁体   English

如何在 ASP.NET Core 中将控制器重定向到剃刀页面

[英]How to Redirect Controller to razor page in ASP.NET Core

I send data from Index.cshtml to my controller.我将数据从Index.cshtml发送到我的控制器。 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).然后我想从控制器重定向到Admin文件夹内的index.cshtml页面(这两个是两个不同的index.cstml文件)。 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:文件夹结构:

文件夹结构以便更好地理解

How about this?这个怎么样?

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

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/Chats/Index.cshtml");

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

In the code above you would replace the argument with wherever your page is located.在上面的代码中,您可以将参数替换为页面所在的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM