2010-03-23 33 views
2

我想在IIS 7的託管模塊中執行C#中的幀重定向。
當我呼叫context.Response.Redirect(@"http://www.myRedirect.org");時,顯示正確的頁面,但地址顯示在瀏覽器中。這正是我不想要的。
所以,我想是這樣的:在C中的幀重定向#

private void OnBeginRequest(object sender, EventArgs e) 
{ 
    HttpApplication app = (HttpApplication)sender; 
    HttpContext context = app.Context; 

    // make a frame redirect if a specified page is called 
    if (context.Request.ServerVariable["HTTP_REFERER"].Equals(@"http://www.myPage.org/1.html")) 
    { 
     // perform the frame redirect here, but how? 
     // so something like 
     context.Response.Redirect(@"http://www.myRedirect.org"); 
     // but as I said that doesn't redirect as I want it to be 
    } 
} 

有關的任何想法?
編輯: 我試過的例子,所以我必須:

private void OnBeginRequest(object sender, EventArgs e) 
{ 
    HttpApplication app = (HttpApplication)sender; 
    HttpContext context = app.Context; 

    // make a frame redirect if a specified page is called 
    if (context.Request.ServerVariable["HTTP_REFERER"].Equals(@"http://www.myPage.org/1.html")) 
    { 
     // perform the frame redirect here, but how? 
     context.Response.Write(@"<html>"); 
     context.Response.Write(@"<head>"); 
     context.Response.Write(@"</head>"); 
     context.Response.Write(@"<frameset rows=""100%,*"" framespacing=""0"" frameborder=""NO"" border=""0"">"); 
     context.Response.Write(@"<frame src=""http://www.myRedirect.org"" scrolling=""auto"">"); 
     context.Response.Write(@"</frameset>"); 
     context.Response.Write(@"<noframes>"); 
     context.Response.Write(@"<body>Some text..."); 
     context.Response.Write(@"</body>"); 
     context.Response.Write(@"</noframes>"); 
     context.Response.Write(@"</html>"); 
    } 
} 

但也不能正確重定向。我仍然有我的瀏覽器中顯示的重定向地址。那麼還有其他想法嗎?編輯: 我明顯犯了一個錯誤。上面的代碼工作,並做我想要的。它首先沒有工作,因爲我的重定向網址正在做一些意想不到的事情。

回答

1

要執行frame redirect,您需要發回包含單個框架的框架集的HTML代碼,並將其源設置爲http://www.myRedirect.org。就服務器和瀏覽器而言,沒有發生重定向 - 它只是收到了一些HTML代碼。

正如您所看到的,執行Response.Redirect會導致瀏覽器對新頁面發出新的新請求,並在標題欄中向用戶顯示新地址。它通常用於頁面實際更改其地址時,但所有者仍然希望它可以從原始URL訪問。

編輯:HTML框架重定向示例:http://en.wikipedia.org/wiki/URL_redirection#Frame_redirects

+0

好吧,請你給我一些代碼例子嗎? – 2010-03-23 08:53:42

+0

在發送HTML代碼的維基百科文章(鏈接到我的文章中的「框架重定向」一詞)中有一個很好的例子,但我從來沒有寫過服務器模塊,所以我不知道你會怎麼做。也許用'Response.Write(... HTML代碼...)'? – 2010-03-23 08:55:51