来自Request.Url.ToString()的字符串在操作/比较第一个字符时大量更改为另一个字符串

Gas*_*a79 9 c# asp.net string

我知道有更简单的方法来做到这一点并相信我,我已经尝试过了.我当然愿意接受任何建议=).您不需要阅读整个代码,只需要说明问题所在的部分.另外,我正在调试perl风格,所以你们可以看到.哦,我是否提到过,在我的开发环境中,一切都按预期工作?

这是代码:

string GetPortalAlias()
{
    String myURL2 = Request.Url.ToString();
    URLLabel.Text = "Original Request.Url.ToString() returned: \"" + myURL2 + "\"";
    string myURL = string.Copy(myURL2);
    URLLabel.Text = "Copying it to myURL, it's now: \"" + myURL + "\"";
    myURL = myURL.ToLower().Trim();
    URLLabel.Text += "<br>Trimming and ToLower myURL.<br>The new url is \"" + myURL + "\"" + "<br>";
    myURL = myURL.Replace(":80", "");
    URLLabel.Text += "Replacing the \":80\".<br> The new url is\"" + myURL + "\"<br>";


    //***HERE LIES THE PROBLEM***
    myURL = myURL.Replace("http://", "");
    URLLabel.Text += "Replacing the \"http://\".<br> The new url is\"" + myURL + "\"<br>";
    //***PROBLEM ENDS***


    myURL = myURL.Remove(myURL.IndexOf("/"));
    URLLabel.Text += "Removing everything after the \"/\"." + "<br> The new url is \"" + myURL + "\"<br>";
    URLLabel.Text += "<br>GetPortalAlias Returning \"" + myURL + "\"";
    return myURL;
}
Run Code Online (Sandbox Code Playgroud)

信不信由你,网页上产生的输出是这样的:

产量

Copying it to myURL, it's now: "http://sar.smg.com.ar/Default.aspx?TabID=912"
Trimming and ToLower myURL.
The new url is "http://sar.smg.com.ar/default.aspx?tabid=912"
Replacing the ":80".
The new url is"http://sar.smg.com.ar/default.aspx?tabid=912"
Replacing the "http://".
The new url is"intranetqa/default.aspx?tabid=912"
Removing everything after the "/".
The new url is "intranetqa"

GetPortalAlias Returning "intranetqa" 

所以......出于某种原因,当它到达替换部分时,它会神秘地改变为以"intranetqa"而不是"sar.smg.com.ar"开头."intranetqa"是我们的默认主机名.改变或采取HTTP的任何特征://以任何方式突破字符串.

我这样做是string.copy因为我知道如果两个字符串相等,编译器会将它们存储在同一个地方,因此我想防止错误.把这些线路拿走并Request.Url.ToString()用来myURL直接做什么都没做.他们只是一个测试,看看是否有效.

这是我尝试过的事情列表:

  • string/的所有组合String都没有效果.
  • 我试过Request.Host.Url,它只是给了我"intranetqa".
  • 我用过Request.Url.AbsoluteUri,这就是为什么我有替换:80线.
  • 使用这些.tochararray功能给我带回了内心的感觉
  • myURL = myURL.Substring(6)返回intranetqa的东西.
  • string.Contains("sar.smg.com.ar")返回false.

我相信诀窍就在这里:

  • Uri uriAddress1 = Request.Url;"The parts are <br>" + "Part 1: " + uriAddress1.Segments[0] + "<br>Part 2: " + uriAddress1.Segments[1];给出Part1:"/"和第2部分:"Default.aspx".尝试访问第3部分(索引2)会产生异常.request.url没有第一部分,但是当我调用ToString()方法时,它确实有一个"假的"第一部分

Dam*_*ver 11

浏览器和服务器之间是反向代理和输出重写器.这些可以是相同的组件,也可以是单独的组件.

您的服务器实际看到的URL始终是表单http://intranetqa/default.aspx?tabid=912(在反向代理/ URL重写器截获请求之后).

您的服务器产生的输出实际上是:

Copying it to myURL, it's now: "http://intranetqa/Default.aspx?TabID=912"
Trimming and ToLower myURL.
The new url is "http://intranetqa/default.aspx?tabid=912"
Replacing the ":80".
The new url is"http://intranetqa/default.aspx?tabid=912"
Replacing the "http://".
The new url is"intranetqa/default.aspx?tabid=912"
Removing everything after the "/".
The new url is "intranetqa"

GetPortalAlias Returning "intranetqa" 
Run Code Online (Sandbox Code Playgroud)

输出重写器会检查您的服务器的输出,并做了替换http://intranetqahttp://sar.smg.com.ar.一旦你http://脱掉这些琴弦的正面,它就不再匹配,因此不再发生更换.

如果您想知道原始请求URL /主机是什么,希望反向代理可以或可以配置为使用原始URL向请求添加额外的头.

  • 所以,基本上,字符串中一直都有`http:// intranetqa`,但每当它显示该链接时,它就被转换为`http:// sar.smg.com.ar` ......直到"http" ://"已经消失,它不再被识别?哇.这是一个令人印象深刻的演绎. (2认同)
  • @Bobson - 奇怪的部分是,一旦我看到它,我认为它是显而易见的.我确信当我完成通勤回家时,其他人会发布相同的答案. (2认同)