actionresult to refresh current page

17,292

Solution 1

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.

Solution 2

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());
}
Share:
17,292
objectbox
Author by

objectbox

Updated on August 27, 2022

Comments

  • objectbox
    objectbox over 1 year

    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