Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: enter image description here

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?

like image 244
Pinte Dani Avatar asked Jun 27 '18 09:06

Pinte Dani


People also ask

Which is correct syntax for RedirectToAction?

RedirectToAction(String, String, RouteValueDictionary) Redirects to the specified action using the action name, controller name, and route values.

What is PageModel in ASP.NET Core?

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.

What is OnPostAsync?

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.


1 Answers

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.

like image 106
CodeNotFound Avatar answered Oct 22 '22 02:10

CodeNotFound