gpt4 book ai didi

asp.net-mvc - ASP.NET核心2 : how to RedirectToPage with area?

转载 作者:行者123 更新时间:2023-12-02 05:51:24 28 4
gpt4 key购买 nike

RedirectToPage("Companies") 将重定向到 /Pages/Companies.cshtml(从 ASP.NET MVC Controller )

但是如果想重定向到此页面 /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); 配置的 Razor 页面没有添加特定的路由。

最佳答案

使用overload of RedirectToPage that takes an object representing RouteValues :

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

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

关于asp.net-mvc - ASP.NET核心2 : how to RedirectToPage with area?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52107626/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com