提问者:小点点

asp:超链接导航url向url添加相对路径


我在aspx页面中有一个Asp:Hyperlink,我正在动态设置文本和导航url,但当页面呈现时,它会在呈现的href中添加我的网站的相对路径。我不知道为什么?

<asp:HyperLink runat="server" ID="charityNameText"></asp:HyperLink>
//Getting data from database

charityNameText.Text = entryDetails.RegisteredCharityName;
charityNameText.NavigateUrl = "www.facebook.com";
charityNameText.Target = "_blank";
<a id="ctl00_PageContent_CompetitionsEntries_ctl06_charityNameText" href="../../ConLib/Custom/www.facebook.com" target="_blank">save the childrens</a>


../../ConLib/Custom/ is the path where this file is located.

施救


共3个答案

匿名用户

你的情况有不同的解决方案。我最好的方法是使用系统。UriBuilder类。

String myUrl = "www.facebook.com";
UriBuilder builder = new UriBuilder(myUrl);
charityNameText.NavigateUrl = builder.Uri.AbsoluteUri;

UriBuilder将您的案例中的协议(HTTP)添加到您正在加载的URL中,并使用完整的URL初始化Uri类的实例。使用AbsolteUri属性。

对于更复杂的情况,可以使用正则表达式:

        String myUrl = "www.facebook.com";
        System.Text.RegularExpressions.Regex url = new System.Text.RegularExpressions.Regex(@"/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

        System.Text.RegularExpressions.MatchCollection matches = url.Matches(myUrl);

        foreach (System.Text.RegularExpressions.Match match in matches)
        {
            string matchedUrl = match.Groups["url"].Value;
            Uri uri = new UriBuilder(matchedUrl).Uri;
            myUrl = myUrl.Replace(matchedUrl, uri.AbsoluteUri);
        }

匿名用户

您必须将协议添加到URL的开头:http://wwww.facebook.com

匿名用户

我觉得你应该用http://www.facebook.com

希望有帮助