ASP.NET Core 2:如何使用区域RedirectToPage?

问题描述 投票:4回答:2

RedirectToPage("Companies")将重定向到/Pages/Companies.cshtml(来自ASP.NET MVC控制器)

但是,如果想重定向到这个页面/Areas/MyArea/Pages/Companies.cshtml怎么办?

所有这些和许多其他人都不起作用:

RedirectToPage("/MyArea/Companies.cshtml") 
RedirectToPage("MyArea/Companies.cshtml") 
RedirectToPage("./MyArea/Companies.cshtml") 
RedirectToPage("/MyArea/Companies") 
RedirectToPage("MyArea/Companies") 
RedirectToPage("./MyArea/Companies") 

有时我会收到“找不到页面”错误。有时得到“指定带有前导'/'的根相对路径以生成Razor页面之外的URL”。没有Pages文件夹。我知道这一切都可以再次改变所有规则。

附:使用普通.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)配置的剃刀页面;没有添加特定路由。

asp.net-mvc asp.net-core-2.0 razor-pages asp.net-core-2.1
2个回答
8
投票

使用overload of RedirectToPage that takes an object representing RouteValues

return RedirectToPage("/Companies", new { area = "MyArea" });

请注意,如果在控制器中使用'/'(或在Razor页面之外的任何位置),则需要使用RedirectToPage。否则它不是必需的(但仍然有效)。


0
投票

这对我有用:

return RedirectToPage("/Companies", new { area = "MyArea" });

在普通的.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);下工作没有配置特定的路由。

我觉得这将是一个受欢迎的问题......感谢Mike Bring,他向我展示了一条道路。

附:如果您有Pages文件夹 - 所有规则将再次更改。这就是“Razor Pages”试图从“MVC魔术”中跑出来的方式

© www.soinside.com 2019 - 2024. All rights reserved.