【问题标题】:Input URL like http://example.com changes to http:/example.com in action input输入 URL,如 http://example.com 在操作输入中更改为 http:/example.com
【发布时间】:2023-03-14 08:41:01
【问题描述】:

我问了一个问题以获取 URL 作为操作输入 here。现在我有一个新问题。传递给操作的 URL 从 http://example.com 更改为 http:/example.com

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

P.S:我添加了这个代码来解决,但我认为将来可能会有另一个问题!代码是:

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

【问题讨论】:

  • 在不相关的注释上,您的代码似乎正在将所有沮丧/未决定的面孔替换为倾斜的、害怕的面孔。

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


【解决方案1】:

使用正则表达式:

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

如果您需要还原:

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

【讨论】:

  • 你知道为什么会这样吗?
  • 是的,我的代码正确替换了任何字符串中的 href 斜杠,如果字符串类似于 http://google.com http:/google.com,则您的代码错误
  • 谢谢。我会用你的方式,但我想知道为什么。很抱歉没有将您的答案设置为已接受!
【解决方案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;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多