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?
RedirectToAction(String, String, RouteValueDictionary) Redirects to the specified action using the action name, controller name, and route values.
PageModel is a feature of ASP.NET Core that is used to make possible the separation of concerns between the UI layer and the data logic layer directly with a code-behind file without the controller.
The OnPostAsync handler method calls the Page helper method. Page returns an instance of PageResult. Returning Page is similar to how actions in controllers return View . PageResult is the default return type for a handler method. A handler method that returns void renders the page.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With