Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

No more Meta Refresh Tags

On this page:

From time to time I have pages that need to auto-refresh or more commonly a page that automatically needs to go to another page after showing a short informational message. In the past I’ve always done this with Meta-Refresh tags in the page which looks like this:

<html xmlns="http://www.w3.org/1999/xhtml">

    <title> Welcome Back</title>
    <meta http-equiv="Refresh" content="2; URL=/wwstore/Profile" />

Using ASP.NET (WebForms) Code to add the Meta Header

You can also do this in code in an ASP.NET WebForms page by adding an HtmlMeta() control to the Page.Header.Controls collection like this:

// Classic ASP.NET - WebForms

// *** Create META tag and add to header controls
HtmlMeta RedirectMetaTag = new HtmlMeta();
RedirectMetaTag.HttpEquiv = "Refresh";

RedirectMetaTag.Content = string.Format("{0}; URL={1}", this.Context.Items["ErrorMessage_Timeout"], NewUrl);


Sending HTTP Headers directly

But I never put 2 and 2 together to realize that the meta tag is actually mapping an actual HTTP Header. Meta tags are essentially page level overrides to actual Http headers, so meta-refresh refers to the Refresh Http header.

So, from a server generated application, it's actually much easier to add a refresh by adding the following to the Response headers like this:

Response.AppendHeader("Refresh", "4");

This refreshes the current page after 4 seconds by reloading itself.

Alternately to go to a different page:

Response.AppendHeader("Refresh", "4; url=/profile");

This is of course much shorter and cleaner. One advantage of the Meta tag approach is that if somebody bookmarks the page the Meta-Refresh will still fire even on a page that is browser cached, while the HTTP header most likely wouldn’t.

Where can you do this?

Adding Http headers via Response.AppendHeader() can be done inside of the ASP.NET request processing. Since ASP.NET always caches responses you can delay adding the header until the very end of a request which allows you to conditionally set it based on Page logic.

Response.AppendHeader() is also available in HttpHandlers and HttpModuless where the same up until the end rules apply.

Note: ASP.NET Core and other Web platforms do not cache headers and require that headers are written before any other content is sent to the Response.

Ah, the little things that one can easily overlook that sometimes make things easier...

Posted in .NET  

The Voices of Reason


Peter Bromberg
August 04, 2006

# re: No more Meta Refresh Tags

Well! That was, uh -- Refreshing!

August 06, 2006

# re: No more Meta Refresh Tags

does it work with firefox ? seems here it doesnt ...

Rick Strahl
August 06, 2006

# re: No more Meta Refresh Tags

Yes it does here... I also wasn't sure so I tried all the browsers I had installed which is IE 7, FF 1.5, Opera 9.0.

You'll want to make sure if you're redirecting to another page the page URL is correct. So you may have to use ResolveUrl() to get the URL reference right.

And if you're not going to another page - make sure you actually notice when the page changes <g>... on a couple of my pages the refresh is so quick the page barely blinks <s>...

Oskar Austegard
August 07, 2006

# re: No more Meta Refresh Tags

This also works nicely in ASP.NET 1.1.

Couple this with the Fajax trick (http://secretgeek.net/fajax.asp) and you have real fake ajax on the cheap.

August 17, 2006

# re: No more Meta Refresh Tags

hey Rick, could you give an opinion about that Fajax trick?...do you see any value in using that..or is that just going to come across as a cheap gimmic to the enduser...like having an animated cartoon character doing the robot on your web page.

It's not cross browser capatible...but it also doesn't really break anything in FF.


Rick Strahl
August 17, 2006

# re: No more Meta Refresh Tags

I don't see anything wrong with using this trick as long as you realize that you are in fact not doing anything AJAX. If you can make the user experience better by all means do it <g>...

# DotNetSlackers: No more Meta Refresh Tags

November 21, 2006

# re: No more Meta Refresh Tags


I tried your trick in asp.net 2.0 with MS Ajax 2.0. Inside an Update Panel I insert a GridView.

The first time that the page is render, the "refresh" works fine, but if you press something inside the gridview that cause a postback, the refresh tag is not triggered.

What could be the problem?

Thanks in advance.

Rick Strahl
November 21, 2006

# re: No more Meta Refresh Tags

HTTP Headers can't be updated in an AJAX callback. The page is already there, so you can't change the header for that page.

If you want the page to refresh you have to use client side code with a timer to change location and hook that through the script manager:

Something like this:

this.ScriptManager.RegisterStartupScript(this.Type(), "Refresh", "window.setTimeout('window.location=''thisPage.aspx''',5000)",true);

November 23, 2006

# re: No more Meta Refresh Tags

Thanks for your answer Rick, but my situation is a little bit more complicated, because I included your code to inject the javascript "refresh", but my purpose is to reset the refresh timeout every time that a client raises a postback.

For example:

You have your session timeout defined in 10 minutes. And your script will refresh and redirect your page to another when a user browse your asp page without do nothing during 10 minutes. But suppose that the user press something that initiates a postback after 9 minutes. What I need is to reset the interval again, otherwise in 1 minute the refresh javascript will be raised and I dont want that.

Rick Strahl
November 23, 2006

# re: No more Meta Refresh Tags

If you're doing a postback you can just reset the Refresh header on the postback. If you're doing a Callback then you can use the code I showed above. That should handle either situation.

January 12, 2007

# re: No more Meta Refresh Tags

how would you handle a situation where the page with the desired timeout is fully encapsulated in an ajax .net update panel - and therefore the controls on the page only cause (partial) postbacks - which dont actually reset the timeout..?

neither the meta tag nor the window.setTimeout method seem to work. the user can continuously be manipulating the controls - and apparently because there's no full-postbacks - the timeouts keep kicking in.

any ideas would be appreciated-


Rick Strahl
January 13, 2007

# re: No more Meta Refresh Tags

If you're using AJAX on the page you can use setTimeout() and manipulate the location object in the callback handler.

The header of the page obviously won't be changed if you're doing partial postbacks since hte page itself including all of its headers don't change. I seem to remember some talk that there's some logic in UpdatePanel that does deal with Response.Redirect() calls, but I haven't tried that because frankly that seems silly in that it's very un-Ajax like. It's best to deal with these kind of tasks via client script in Ajax code.

# どっとねっとふぁんBlog : MetaタグでRefreshの代わりにHttpヘッダーでRefresh

ASP.NET Forums
July 05, 2007

# Auto Refresh Webpage - ASP.NET Forums

Muthu kumaran
August 22, 2007

# re: No more Meta Refresh Tags

Cool code man.. Thanks for your code..

October 19, 2007

# re: No more Meta Refresh Tags

This is quite intresting post.
But it would have been a great help if you have explained a fewer terms for some novice programmers.
RedirectMetaTag.Content = string.Format("{0}; URL={1}", this.Context.Items["ErrorMessage_Timeout"], NewUrl);

What does "this.Context.Items["ErrorMessage_Timeout"]" mean?
& what is "NewUrl"?

March 22, 2008

# re: No more Meta Refresh Tags

wow! exactly what i need
what a clean way of doing things :D

May 22, 2009

# re: No more Meta Refresh Tags

This eliminated a LOT of code from my page. THANKS!

Mike Kingscott
December 16, 2009

# re: No more Meta Refresh Tags

Hmmm. I wonder how the Response.AppendHeader would be liked by WorldPay? We've recently been advised to use a META refresh to redirect people who land on our payment result page (hosted by WorldPay), as they no longer allow scripted redirects (in addition to response.redirects, or server.transfers) as part of their recent XSS changes. If, as you say, appending the redirect to the Response does the same thing, it's a lot less code, but I'll guess I'll have to suck it and see.

Rick Strahl
December 16, 2009

# re: No more Meta Refresh Tags

@Mike - it's the same thing. The idea of a Refresh is that the browser navigates. The target site sees the same thing.

January 27, 2010

# re: No more Meta Refresh Tags

Great example. However I couldnt get it to work without adding:


before the

Response.AppendHeader("Refresh", "4; url=profile.aspx");


October 19, 2010

# re: No more Meta Refresh Tags

Still not working for me.....
Can u please tell me how to clear values of meta tag!!!!!!!!!!!
I t is really important for me !!!!!!!!!!

April 16, 2012

# re: No more Meta Refresh Tags

Awesome! Works well.

William Walseth
May 03, 2012

# re: No more Meta Refresh Tags

The Refresh in IE 9 appears to work in "seconds", not minutes.
Here's my simple auto log-out code
Session.Timeout = 10; // time-out in 10 minutes...
Response.AppendHeader("Refresh", "600; url=logon.aspx"); // re-direct after time-out

July 08, 2012

# re: No more Meta Refresh Tags

Superb Code man!!!! Thanks allot!! Exactly this was what i need...

August 02, 2012

# re: No more Meta Refresh Tags

I tried the example but it only works the first time. I need time to refresh the session because I have a Silverlight application hosted on the page and never generates a postback or refresh every 20 minutes exactly But it refreshes the page and lose all the work they had done.

March 28, 2013

# re: No more Meta Refresh Tags

The one line of code that you have in your punchline... Is that to be placed in the Page_Load event if I want the page to refresh at regular intervals? Is there code I can add to reset or inhibit the refresh in page control events? Like RowEditing or something like that? Thanks!

Rick Strahl
March 28, 2013

# re: No more Meta Refresh Tags

@CleveSteve - you can put this anywhere really. Headers aren't sent until the page rendering completes since it's all buffered.

Joshua H
July 25, 2023

# re: No more Meta Refresh Tags

@Rick 17 yrs later this page was still helpfull.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2023