映射到不同域的路径

时间:2011-07-28 02:48:36

标签: c# asp.net redirect

我希望能够将路径映射到其他域,但将原始地址保留在地址栏中。有没有办法对此进行编码或是否在IIS中进行编码。如果可能的话,我最好想要一种编码方法。

我有一个链接,其href类似于“http://www.example.com/invproxy.aspx?id=1&batch=1”。 我希望将该地图设为“http://www.otherdomain.com/Files/TheFileRequest.aspx”。

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

由于

编辑:我已经解决了这个问题(可能不是最经济的方法),但下面是我在invproxy.aspx的Page_load事件中使用的代码

// 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);

编辑:只是添加完成的结果,我将我的PDF文档显示在浏览器的新选项卡中(兼容),地址栏中显示http://www.example.com/invproxy?myquerystring

2 个答案:

答案 0 :(得分:1)

根据上面的回复,我建议使用IFrame加载您希望最终用户查看其网址的页面。请记住,这可能会产生连锁效应,具体取决于所包含页面的复杂程度。

答案 1 :(得分: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);

..你可以让它更复杂..只是告诉你如何在必要时检索当前URL中的查询字符串参数和值