【问题标题】:Appending string to URL将字符串附加到 URL
【发布时间】:2013-03-29 04:35:55
【问题描述】:

我正在尝试修改服务器端的 DIV 标签内容。

string url = "www.mypage.com";
string html = "<a href='http://www.facebook.com/sharer.php?u='"+url+" title='Submit to Facebook' target='blank'><img src='http://itprism.com/plugins/content/itpsocialbuttons/images/big/facebook.png' alt='Submit to Facebook'></a>" +
"<a href='http://twitter.com/share?text=Atlas Paving &url='" + url + " title='Submit to Twitter' target='blank'><img src='http://itprism.com/plugins/content/itpsocialbuttons/images/big/twitter.png' alt='Submit to Twitter'></a>";

divSocial.InnerHtml = html;

当我尝试导航到附加的 URL 时,我可以看到该 URL 没有正确生成。附加的网址为空。这里有什么问题

【问题讨论】:

    标签: c# asp.net url


    【解决方案1】:

    您的结束语 ' 不正确。它应该在url之后关闭:

    string html = "<a href='http://www.facebook.com/sharer.php?u=" + url + "' title= ...
    

    避免此类难以发现的错误并使代码更易于阅读的一个好习惯是使用string.Format

    string html = string.Format("<a href='http://www.facebook.com/sharer.php?u={0}' title= ...", url);
    

    【讨论】:

      【解决方案2】:

      你可以试试:

      string html = "<a href='http://www.facebook.com/sharer.php?u="+url+"' title='Submit to Facebook' target='blank'>
      

      代替:

      <a href='http://www.facebook.com/sharer.php?u='"+url+" title='Submit to Facebook' target='blank'>
      

      我认为你关闭你的 href 属性有点太早了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-25
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多