ASP.NET Core 2: how to RedirectToPage with area?

Viewed 9747

RedirectToPage("Companies") will redirect to /Pages/Companies.cshtml (from an ASP.NET MVC controller)

But what if want redirect to this page /Areas/MyArea/Pages/Companies.cshtml ?

All those and many others don't work:

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

Sometimes I get "Page not found" error. Sometimes get "Specify a root relative path with a leading '/' to generate a URL outside of a Razor Page". There are no Pages folder. I know all this can change all rules again.

P.S. Razor pages configred with plain .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); no specific routing added.

2 Answers

This works for me:

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

Works under plain .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); no specific routing configured.

I feel this will be a popular question... Thanks to Mike Bring, he show me a path.

P.S. If you have Pages folder - all rules will be changed once more. That is the way how "Razor Pages" try to run out from "MVC magic"

Related