2010-03-23 34 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