2011-07-28 96 views
0

我希望能够将路径映射到不同的域,但保留地址栏中的原始地址。有没有一种方法来编码或在IIS中。如果可能的话,我最好喜欢一种编码方法。映射到不同域的路径

我有一个链接,其href是这样的“http://www.example.com/invproxy.aspx?id=1 &批= 1”。 我想要那张地图到“http://www.otherdomain.com/Files/TheFileRequest.aspx”。

我生成的映射路径使用原始请求的查询字符串。它适用于Server.Transfer或Response.Redirect但我想要地址栏仍然说最初请求的URL。这可能吗?

感谢

编辑:我已经解决了这个问题(可能不是最经济的方式),但下面是我使用的invproxy.aspx

// Just test file, no code to select path yet. 
string requestPath = "http://www.otherdomain.com/Files/TestFile.pdf"; 

// Setup the request for the PDF File 
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(requestPath); 

// Get the response from otherdomain.com 
HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 

// Assign the invproxy response the PDF ContentType 
Response.ContentType = "application/pdf"; 

// Get the body of the response in a stream object 
Stream s = resp.GetResponseStream(); 

// Create a byte array to be read into from the stream 
byte[] buffer = new byte[resp.ContentLength]; 

int position = 0; 
while (true) 
{ 
    //Read bytes one by one, slow but causes errors trying to read the whole thing. 
    //I need to use chunks here. 
    int b = s.ReadByte(); 
    if (b == -1) 
    { 
     //This is the end of the stream 
     break; 
    } 
    else 
    { 
     //Set the byte at the current position to the value just read 
     buffer[position] = (byte)b; 

     //Advance the position by one. 
     position++; 
    } 
} 
//breakpoint debugging 
string sa = Encoding.Default.GetString(buffer); 

//Write the file to the invproxy response. 
Response.BinaryWrite(buffer); 

编辑的Page_Load事件的代码:只需添加完成的结果,我得到我的PDF文档显示在浏览器中的新标签(在兼容)与地址栏说http://www.example.com/invproxy?myquerystring

+0

是否otherdomain.com住在如何检索当前URL查询字符串参数和值同一台服务器,还是你控制它? –

+0

是的,他们都在我的VPS上运行,我控制他们两个。 –

+0

我可能并不完全理解你正在尝试做什么,但是这种技术应该会给你目前的URL – MacGyver

回答

1

根据上面的响应,我会建议使用IFrame加载第t页您希望在您想要最终用户看到的网页的页面内使用帽子。请记住,这可能会产生连锁效应,具体取决于所包含页面的复杂程度。

+0

好的,所以为了使它复杂一点,所服务的实际内容将会是一个PDF文件。对于没有内置PDF查看器的浏览器,这不是一个问题,因为该文件只是下载而不是在页面中打开。但对于支持它的浏览器,我想显示PDF,但具有上面的URL。我在我的链接上使用target =“_ blank”,所以总是打开一个新标签页。 –

+0

这应该仍然有效,但如果这是您正在尝试完成的全部内容,那么您可以将内容类型更改为application/pdf并将PDF字节流式传输到Response。这里是如何做流的链接:http://support.microsoft.com/kb/306654 –

+1

PDFs可以通过3种方式向用户提供...一般使用PDFViewer.aspx ..你蒸汽字节(也就是PDF文件)给用户..使用带* .pdf扩展名的HTTP处理程序来提供文件本身......或者将PDFViewer嵌入到对象中 - 作为严格的HTML(或iframe - 过渡HTML).. if你需要的例子,我有一些,但他们在VB.NET ..虽然有一个工具,非常容易获得在C#中的代码 – MacGyver

0

试试这个:

string newParamName = "QueryParam"; 
string newParamValue = HttpUtility.UrlEncode(Request.QueryString("queryValue").ToString()); 
Uri uri = HttpContext.Current.Request.Url; 
string url = uri.Scheme + "://" + "www.otherdomain.com" + "/Files/TheFileRequest.aspx" + "?" + newParamName + "=" + newParamValue; 
HttpContext.Current.Response.Redirect(url); 

..你可以让它更复杂..只是显示你如果有必要