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?
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.