ASP.net MVC core RedirectToPage error - specify root relative path error

大城市里の小女人 提交于 2020-03-21 19:08:08

问题


I want to redirect to a razor page from a normal controller action like this:

return RedirectToPage("Edit", new { id = blogId });

I have already a razor page named "Edit" which is working when navigating to it normally:

With RedirectToPage I get the following error:

InvalidOperationException: The relative page path 'Edit' can only be used while executing a Razor Page. Specify a root relative path with a leading '/' to generate a URL outside of a Razor Page.

Any idea how to specify that path?


回答1:


The error already gave you the answer: You should add the leading '/' at the start and specify a relative path to your razor page. So you should have

return RedirectToPage("/BlogPosts/Edit", new { id = blogId });

Instead of

return RedirectToPage("Edit", new { id = blogId });

Notice the difference between "/BlogPosts/Edit" and "Edit". RedirectToPage method expects a path to your razor page (based on your image the relative path is "/BlogPosts/Edit") starting to the root folder which is Pages by default.

Note: Starting with Razor Pages 2.0.0, redirecting to "sibling" pages works as well. In other words, if you have a page at /BlogPosts/View, it could redirect to /BlogPosts/Edit with RedirectToPage("Edit", new { id = blogId }), without specifying a rooted path.



来源:https://stackoverflow.com/questions/51058793/asp-net-mvc-core-redirecttopage-error-specify-root-relative-path-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!