简体   繁体   中英

Where should a classic ASP redirect go?

Should the code below go in the <head> ? Or should there be nothing else in the page besides the code below?

<%
    Response.Redirect "http://www.sitename.com/?" & Request.QueryString
%>

As long as you have response buffering enabled and you haven't called Response.Flush , you can perform a Response.Redirect anywhere in the page. Buffering causes all ASP code to be executed before any of the page is sent to the browser. Thus if you perform a redirect within the code after <head> , none of the HTML will be sent to the browser and thus the redirect header will be sent correctly.

Buffering is typically enabled by default in IIS these days. To be safe, you could include this line near the top of your ASP page:

Response.Buffer = True

Classic ASP is server side code, <head> along with all html is client side code. If you have this line in a .asp page then there's no point in having anything else on the page as users will never get a chance to see it.

That line would make more sense if it was inside a conditional statement - eg

<%
    If Request.QueryString("id") <> "" then
    Response.Redirect "http://www.sitename.com/?" & Request.QueryString("id")
    End If
%>

Here the user would only be bounced to another page if a value for id was provided in the url, otherwise any client side code in the page would be sent to the browser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM