Input URL like http://example.com changes to http:/example.com in action input

混江龙づ霸主 提交于 2020-01-05 07:10:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!