问题
I asked a question to get URL as action input here. Now I have a new problem. The passed URL to action changes from http://example.com
to http:/example.com
.
I want to know why and how can I resolve the problem.
P.S: I added this code to resolve but I think there may be another problems in future! the code is:
if ((url.Contains(":/")) && !(url.Contains("://")))
{
url = url.Replace(":/", "://");
}
回答1:
The browser (or server) is replacing a double slash (illegal) with a single one.
Try it,
http://stackoverflow.com/questions/11853025//input-url-like-http-site-com-changes-to-http-site-com-in-action-input
(in Chrome) goes to:
http://stackoverflow.com/questions/11853025/input-url-like-http-site-com-changes-to-http-site-com-in-action-input
If I were you, I would remove the http:// from your path and add it later.
http://localhost:1619/Utility/PR/example.com/
Then, url = "http://" + url;
If you might get secure urls, add that to the route /http/example.com
or /https/example.com
回答2:
use regex:
string src = @"http://example.com";
string result = Regex.Replace(src, @"(?<=https?:/)/", "");
if you need to revert:
string src = @"http:/example.com";
string result = Regex.Replace(src, @"(?<=https?:)/(?=[^/])", @"//");
来源:https://stackoverflow.com/questions/11853025/input-url-like-http-example-com-changes-to-http-example-com-in-action-input