4

From some of the action methods I want to return a result that will force current page to refresh.

I wrote this to acquire such result:

 public class RefreshResult : ActionResult {

        public override void ExecuteResult(ControllerContext context) {
            Uri referrer = context.HttpContext.Request.UrlReferrer;
            if(referrer == null || string.IsNullOrEmpty(referrer.AbsoluteUri)) {
                return;
            }
            context.HttpContext.Response.Redirect(referrer.AbsoluteUri);
        }
    } 

In my action methods I simply return new RefreshResult. It works, but I am curious of the possible limitations of such approach. I am not intrested in giving customers an option to access action methods returning such results directly, so I think that I always will be able to refresh current page in such a way. Am I right?

I found this (and couple of other questions) on stackoverflow: ActionResult return to page that called it

But I am more intrested in possible limitations of such approach, not in a "how to".

Thanx in advance

Community
  • 1
  • 1
objectbox
  • 1,281
  • 1
  • 11
  • 13
  • why not simply use RedirectResult? – Rune FS Aug 18 '11 at 12:14
  • in fact I was using RedirectToRouteResult, but I do not like that I need to provide route values to it. with refreshresult I do not need to know url to redirect. – objectbox Aug 18 '11 at 12:41
  • I don't understand your comment. In your code you redirct to a url RedirectResult takes a url Ie return new RedirectResult(referrer.AbsoluteUri); would do the trick no need to create a new class for that was my point – Rune FS Aug 18 '11 at 14:55
  • Ok, I understand your point. I think the reason I use this code is obvious - I do not want to repeat the code in executeresult override everywhere. With this Refresh result I can simply return new RefreshResult - no need to handle the situations when actionmethod is called directly from a browser address bar etc. everywhere – objectbox Aug 18 '11 at 16:41

2 Answers2

8

rouen answer is one way to refresh the page. The other one is to redirect back to the Url that the request was sent from, and there is no need to write implementation yourself, just do it in a normal action in a controller.

The Action might look like this

public ActionResult SomeAction()
{
    //do some work here...

    return Redirect(Request.UrlReferrer.ToString());
}
iAM
  • 1,365
  • 15
  • 25
3

I am not sure what do you mean by "result that will force current page to refresh". If you are executing action on server, you are already "refreshing" the page.

If what you want is some kind of post-redirect-get pattern in order to "land" on original page by GET action again, it is very easy - just implement your custom ActionResult as derived from RedirectToRouteResult (used by RedirectToAction() method on Controller), and supply it with current route values.

Your approach based on referrer is not entirely bad, but keep in mind that referrer is header sent by browser and can by optional in some clients (disabled in browser etc.), while current route values are always available to you.

rouen
  • 5,003
  • 2
  • 25
  • 48