将路径映射到不同的域

本文关键字:路径 映射 | 更新日期: 2023-09-27 18:03:40

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

我有一个链接,其href是这样的"http://www.example.com/invproxy.aspx?id=1&batch=1"。我想把它映射到"http://www.otherdomain.com/Files/TheFileRequest.aspx"

我使用原始请求的查询字符串生成要映射到的路径。它与Server一起工作。转移或响应。重定向,但我希望地址栏仍然说原来请求的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

将路径映射到不同的域

根据上面的响应,我建议使用IFrame来加载您希望最终用户看到其URL的页面内部的页面。请记住,这可能会产生连锁效应,这取决于所包含页面的复杂程度。

试试这个:

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中的查询字符串参数和值