Razor @page路由到其他页面

时间:2020-02-29 08:27:18

标签: asp.net-core razor

我正在使用aspnet core 3。

在控制器中,我的用户可以输入单个URL,例如... / login ...,然后测试它是哪家公司并返回正确的视图-像这样:

[Route("login")]
public IActionResult Login()
{
    return _company == "a" ? View("ThisView") : View("ThatView");
}

但是,如果我使用Razor路由... @页面“ / login” ...我该怎么做?我需要用户键入一个URL,但是然后我需要测试它是哪家公司,然后直接转到正确的剃须刀页面。

1 个答案:

答案 0 :(得分:0)

您可以在剃须刀页面中使用“ RedirectToPageResult()”返回到 相应的页面而不是视图。

前提是您需要创建相应的页面来 返回。

例如,我有一个名为MyTest的页面,用户可以在此页面中输入参数。

如果用户输入“ a”,则返回ThisPage.cshtml,否则,返回ThatThat.cshtml。

MyTest.cshtml:

@page
@model WebApplication1_rzaor_page.MyTestModel
@{
    ViewData["Title"] = "MyTest";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h1>MyTest</h1>
<form asp-page-handler="Login" method="post">
    <input id="Text1" type="text" name="_company" />
    <input id="Submit1" type="submit" value="submit" />
</form>

MyTest.cshtml.cs:

 public class MyTestModel : PageModel
{
    public void OnGet()
    { 
    } 
    public IActionResult OnPostLogin(string _company)
    {
        return _company == "a" ? new RedirectToPageResult("ThisPage") : new RedirectToPageResult("ThatPage"); 
    }
}

这是结果:

enter image description here