What is analog of Response.AddHeader("Refresh", "10") in ASP. NET MVC5

1.7k views Asked by At

Would someone tell me if there is an analog of Response.AddHeader("Refresh", "10") in ASP. NET MVC5, please?

I have tried [OutputCache(NoStore = true, Location = OutputCacheLocation.Client, Duration = 10)] but it does not work.

2

There are 2 answers

0
Jasen On BEST ANSWER

You can use it directly in your controller

public ActionResult MyAction()
{
    Response.AddHeader("Refresh", "10");
    return View();
}

Or you can make a custom action filter

public class RefreshAttribute : ActionFilterAttribute, IActionFilter
{
    public string Duration { get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var duration = 10;

        Int32.TryParse(this.Duration, out duration);            

        filterContext.HttpContext.Response.AddHeader("Refresh", duration.ToString());
    }
}

Usage

[Refresh(Duration = "10")]
public ActionResult MyAction()
{
    return View();
}
0
Chris Pratt On

[OutputCache] is for, well, caching the output of an action. The Duration param merely tells it how long to cache that output. Neither has anything to do with setting HTTP headers, and certainly will not make a page refresh automatically.

Reponse.AddHeader is still valid in MVC5; you just need to ensure that you have not started the response yet. Unless you're doing something off-the-wall, that's not difficult. If you're returning a ViewResult, for example, just call this first:

Response.AddHeader("Refresh", "10");
return View();

If you're directly writing to the response, then just ensure you add the header before you start doing that.