【问题标题】:How do I specify that I want my hyperlink to always be absolute path?如何指定我希望我的超链接始终是绝对路径?
【发布时间】:2011-08-06 00:19:36
【问题描述】:

我有这个代码链接,我在其中使用数据库中的运行时地址填充超链接。

    <asp:HyperLink ID="HyperLink1" runat="server" Target="_blank" 
NavigateUrl='<%#Eval("Source") %>'><%#Eval("Source") %></asp:HyperLink>

问题在于它将链接视为相对链接。所以如果链接是yahoo.com,它会去

http://localhost/yahoo.com

或类似的东西。

如果我的链接源是http://www.yahoo.com,那会起作用,但我不能保证链接开头可能有也可能没有http://。

如何让它始终将其视为绝对 URL。

【问题讨论】:

    标签: asp.net html url


    【解决方案1】:

    你可以试试这样的

    NavigateUrl='<%# GetUrl(Eval("Source")) %>'>
    

    public string GetUrl(object source)
    {
        if(source != null)
        {
            string str = source.ToString();
            return str.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) ?
                               str : 
                               string.Format("http://{0}",str);
        }  
        return string.Empty;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      相关资源
      最近更新 更多