简体   繁体   English

Razor @page 路由到不同的页面

[英]Razor @page routing to a different page

I'm using aspnet core 3.我正在使用 aspnet 核心 3。

In a controller, my user can type in a single url like ... /login ... and then I test which company it is and return the correct view - like this:在控制器中,我的用户可以输入一个 url,如 ... /login ... 然后我测试它是哪家公司并返回正确的视图 - 像这样:

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

But if I use Razor routing ... @page "/login" ... how can I do the same?但是,如果我使用 Razor 路由...@page "/login" ... 我该怎么做呢? I need the user to type a single url, but then I need to test which company it is and then direct to the correct razor page.我需要用户输入一个 url,但随后我需要测试它是哪家公司,然后定向到正确的 razor 页面。

You can use " RedirectToPageResult() " in razor page to return to the corresponding page instead of view.您可以在剃刀页面中使用“ RedirectToPageResult() ”返回相应页面而不是查看。

The premise is that you need to create the corresponding page to return.前提是你需要创建相应的页面才能返回。

For example, I have a page named MyTest, user can input the parameter in this page.例如,我有一个名为 MyTest 的页面,用户可以在此页面中输入参数。

If user enter "a" , then return to ThisPage.cshtml, otherwise, return to ThatPage.cshtml.如果用户输入 "a" ,则返回 ThisPage.cshtml,否则返回 ThatPage.cshtml。

MyTest.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: MyTest.cshtml.cs:

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

Here is the result :结果如下:

在此处输入图片说明

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

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