【问题标题】:Map path to different domain映射到不同域的路径
【发布时间】:2011-07-28 02:48:36
【问题描述】:

我希望能够将路径映射到不同的域,但将原始地址保留在地址栏中。有没有办法对此进行编码,或者是否在 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

【问题讨论】:

  • otherdomain.com 是否存在于同一台服务器上,或者您是否控制它?
  • 是的,它们都在我的 VPS 上运行,我控制它们。
  • 我可能没有 100% 完全理解您要做什么,但该技术应该为您提供当前 URL 的部分

标签: c# asp.net redirect


【解决方案1】:

根据上面的回复,我建议使用IFrame 在您希望最终用户看到其 URL 的页面中加载您真正想要的页面。请记住,这可能会产生连锁反应,具体取决于所包含页面的复杂程度。

【讨论】:

  • 好的,所以稍微复杂一点,实际提供的内容将是 PDF 文档。对于没有内置 PDF 查看器的浏览器,这不是问题,因为文件只会下载而不是在页面中打开。但是对于支持它的浏览器,我想显示 PDF 但具有上述 URL。我在我的链接上使用 target="_blank" 所以总是打开一个新标签。
  • 这应该仍然有效,但如果这就是您想要完成的全部,那么您可以将内容类型更改为 application/pdf 并将 PDF 字节流式传输到响应。这是有关如何进行流式传输的链接:support.microsoft.com/kb/306654
  • PDF 通常可以通过 3 种方式提供给用户...使用 PDFViewer.aspx ..您将字节(又名 PDF 文件)发送给用户 ..使用文件本身提供带有 *.pdf 扩展名的 HTTP 处理程序......或将 PDFViewer 嵌入到对象中——作为严格的 HTML(或 iframe——过渡 HTML)......如果您需要示例,我每个都有一些,但它们在 VB 中.NET .. 虽然有一个工具可以很容易地在 C# 中获取代码
  • 好的,感谢 Chris Shain 和 MacGyver 先生,我得到了答案。我将更新我的帖子以显示我正在使用的代码。
【解决方案2】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多