CurrentUmbracoPage or RedirectToCurrentUmbracoPage?

By Markus Johansson

I had that question in mind when I was working with a new MVC-site using Umbraco 7 and wanted to share my conclusions. Well after some googling I found this great blogpost from the Umbraco core-developer Shannon Deminick. It’s related to Umbraco 5 (RIP) but the concept still applies to the MVC-implementation in Umbraco 7 (and 6), in one of the comments he explains the difference.

So. This is the thing.

Use return CurrentUmbracoPage()

When you just want to return the current page. Here you can use the ViewData and ViewBag objects to pass data from the controller to the view and that will work just fine. Generally, you want to return to the current page if you want to keep the POST-data that was sent to the controller.

Use return RedirectToCurrentUmbracoPage()

When you really want to redirect the user. Shannon writes: “If your POST is successful, you shouldn't just return a response, you should redirect to a page so you can avoid the awful issue of having a user press refresh and have it resubmit the POST”. To pass data back to the view you’ll need to use the TempData-object that will “survive” the redirect.

So if you’re creating a form and the model state IS valid, you should perform your actions and RedirectToCurrentUmbracoPage(), if the model is not valid you should just return the CurrentUmbracoPage(). That’s why many examples look like this:

   return CurrentUmbracoPage()
// do stuff here
return RedirectToCurrentUmbracoPage()

Another thing to notice about the routing is “Just FYI when you POST to a SurfaceController, you are just posting to your own controller which is outside of the Umbraco process, it is just a very normal MVC post. So when you do a return CurrentUmbracoPage call, that then passes execution back to Umbraco to go look up the page that was posted from and renders it. You could in theory just return your own View(model) which will be completely outside the Umbraco process, because it is just an MVC normal controller.

Thanks, Shannon for making this clear! #h5yr

More blog posts

15 januari 2021

Umbraco and MiniProfiler