像http://example.com这样的输入网址在操作输入中更改为http:/example.com

时间:2012-08-07 19:49:38

标签: c# asp.net-mvc c#-4.0 asp.net-mvc-routing asp.net-mvc-4

我问了一个问题,将网址作为操作输入here。现在我遇到了一个新问题。传递的操作网址从http://example.com更改为http:/example.com

我想知道为什么以及如何解决问题。

P.S:我添加了这段代码来解决,但我认为将来可能还有其他问题!代码是:

if ((url.Contains(":/")) && !(url.Contains("://")))
{
    url = url.Replace(":/", "://");
}

2 个答案:

答案 0 :(得分:2)

使用正则表达式:

string src = @"http://example.com";
string result = Regex.Replace(src, @"(?<=https?:/)/", "");

如果您需要还原:

string src = @"http:/example.com";
string result = Regex.Replace(src, @"(?<=https?:)/(?=[^/])", @"//");

答案 1 :(得分:2)

浏览器(或服务器)正在用一个替换双斜杠(非法) 试试吧,

http://stackoverflow.com/questions/11853025//input-url-like-http-site-com-changes-to-http-site-com-in-action-input

(在Chrome中)转到:

http://stackoverflow.com/questions/11853025/input-url-like-http-site-com-changes-to-http-site-com-in-action-input

如果我是你,我会从您的路径中删除http://并稍后添加。

http://localhost:1619/Utility/PR/example.com/

然后,url = "http://" + url;

如果您可能获得安全网址,请将其添加到路线/http/example.com/https/example.com