代码之家  ›  专栏  ›  技术社区  ›  Shyju

ASP.NET页脚控件:跨不同文件夹使用时断开链接

  •  0
  • Shyju  · 技术社区  · 16 年前

    我有一个ASP.NET网站,在那里我有一个index.aspx页面和3个文件夹。也就是说,我的根目录有index.aspx、foldera、folderb,然后是foldercommon(这3个是文件夹)。

    我在foldera中有几个ASP页面,folderb.foldercommon存储JS文件、CSS文件和公共代码etcc…。

    我有一个名为page footer的用户控件,在该控件中保留页面的所有页脚链接。现在我的问题是,当我在索引页中使用根文件夹中提供的同一个页脚用户控件时,它将不适用于其他页面,因为路径不同。因此,我如何重新设计页脚用户控件,以使链接一致?不管文件夹结构或它们所在的位置如何,所有页面上都显示。

    注意:我不想提供到href属性的完整链接(例如: http://sitename/folderA/fielname.aspx )

    有什么想法吗????

    5 回复  |  直到 16 年前
        1
  •  3
  •   Jack Marchetti    16 年前

    如果你在做 asp:Hyperlink ,始终按以下方式设置链接:

    NavigateUrl="~/index.aspx"
    NavigateURL="~/Folder/Default.aspx"
    

    ~基本上是“根”的意思。

    如果你是常客 <a href=""> 然后,您需要根据文件所在的位置提供链接的相对路径。

    因此,如果您在foldera中,并且希望引用根目录。

    <a href="../index.aspx"></a>
    
        2
  •  0
  •   Dewfy    16 年前

    我们有同样的问题。我们使用绝对URL和以下实用程序将现有的“~”转换为正确的路径

    public static class UrlUtils
    {
    
        /// <summary>
        ///This method returns the correct relative path when installing 
        /// the portal on a root web site instead of virtual directory
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetApplicationPath(HttpRequest request)
        {
            string path = string.Empty;
            if (request.ApplicationPath != "/")
                path = request.ApplicationPath;
            return path;
        }
    
        /// <summary>
        /// Changes leading '~' to absolute URL including lead address e.g. http[s]://.... using
        /// <see cref="HttpContext.Current" />.
        /// </summary>
        /// <param name="url">Relative or absolute URL.</param>
        /// <returns>Absolute URL.</returns>
        public static string ResolveAbsoluteUrl(string url)
        {
            if (url.StartsWith("~"))
            {
                HttpRequest request = HttpContext.Current.Request;
    
                string _BaseUrl = new Uri(request.Url.ToString()).GetLeftPart(UriPartial.Authority);
                string baseUrl;
                baseUrl = _BaseUrl + GetApplicationPath(request);
                return baseUrl + url.Substring(1);
            }
            return url;
        }
    
        3
  •  0
  •   Fenton    16 年前

    这可能是你现在拥有的…

    <a href="yourpage.aspx">Link</a>
    

    您可以尝试:

    <a href="/yourpage.aspx">Link</a>
    

    你应该发现斜线是最重要的!

        4
  •  0
  •   spot    16 年前

    我不确定问题的确切性质,但您是否尝试过使用根相对路径?比如:

    "~/Folder/{control or page}"
    

    您的代码示例将非常有用。

        5
  •  0
  •   Yeodave    16 年前

    你能检查一下这个控件所在页面的URL吗?

    Dim currentURL as string = HttpContext.Current.Request.Url.ToString
    

    一旦你知道了网址,你就应该知道要显示哪些链接。

    推荐文章