代码之家  ›  专栏  ›  技术社区  ›  Adam Kiss

不同域上的HTML相关链接

  •  1
  • Adam Kiss  · 技术社区  · 15 年前

    我有Quiji:

    在编写/开发主题时,如何链接到HTML/CSS代码中的各种文件?

    例子: 我们公司主要使用 <base target="http://whatever"> 在我们的主模板中,然后 <img src="./images/file.png"> 在我们的HTML中, "/category/page" 作为链接和类似的东西在我们的CSS中。

    但是,在不同的机器上测试时,我们使用IP地址,而不是编码器的主开发站上的本地主机,因此所有基本链接都不工作(因为本地主机在我们的网络中转到查看机器,而不是编码器)。

    更新页面时也会发生同样的事情——在dev服务器上,我们必须编辑基本目标,这样浏览站点就不会把我们带到活动站点——这部分实际上是相当简单的php( if ... echo else echo something else 但是,它仍然不能解决更多的编码测试问题。

    所以,我的问题是,你如何解决它?如何使用相对链接,这基本上不关心页面在哪个域,也不关心URL重写?(因为 ../images/ 不同于 / 不同的是 /something/somethingElse/page )?

    4 回复  |  直到 15 年前
        1
  •  1
  •   Doug    15 年前

    我很熟悉你上面描述的问题。当您有几个环境、开发、测试、生产等时,这是一个特别的问题。我尝试了上面每个人的建议,但发现它们只是部分有效。我所做的最终解决这个问题是创建一个自定义控件,我称之为“AnchorDomain”。此控件只允许将路径(如“/category/page”或“/images/file.png”)放入其virtulpath属性中,它将根据运行项目的环境动态为您生成正确的URL。通过这种方式,对于我的项目中的所有问题URL,我只需使用这个控件,并且能够独立于环境运行我的代码,并且使用正确的名称解决我的所有URL问题。例如,这里有一个ASPX页面中的语法示例。

    <cc1:AnchorDomain runat="server" Title="My Homepage" Url="default.aspx" UsePageVirtualPath="false" />
    

    因此,在这种情况下,无论您处于何种环境,此链接都将始终显示正确的地址。

    起初,这样做似乎有些过分,但这个控件在我遇到的所有情况下都能解决这个问题。

    见下面的代码:

    享受!

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.ComponentModel;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    namespace Pdc.EventPro.WebControls
    {
      [DefaultProperty("Text"), ToolboxData("<{0}:AnchorDomain1 runat=server></{0}:AnchorDomain1>")]
      public class AnchorDomain : Control
      {
        private string _href = string.Empty;
    
        public AnchorDomain()
        {
          VirtualPath = HttpContext.Current.Request.Path.Substring(0, HttpContext.Current.Request.Path.LastIndexOf("/") + 1);
        }
    
        private string VirtualPath
        {
          get
          {
            return (string)ViewState["virtualPath"];
          }
          set
          {
            ViewState["virtualPath"] = value;
          }
        }
    
        [Bindable(true), Category("Content"), DefaultValue("Performance Development Corporation")]
        public string Title
        {
          get
          {
            return (string)ViewState["title"];
          }
    
          set
          {
            ViewState["title"] = value;
          }
        }
    
        [Bindable(true), Category("Content"), DefaultValue("")]
        public string LinkText
        {
          get
          {
            return (string)ViewState["linktext"];
          }
    
          set
          {
            ViewState["linktext"] = value;
          }
        }
    
        [Bindable(true), Category("Content"), DefaultValue("")]
        public string Url
        {
          get
          {
            return (string)ViewState["url"];
          }
    
          set
          {
            ViewState["url"] = value;
          }
        }
    
        [Bindable(true), Category("Content"), DefaultValue("false")]
        public bool UsePageVirtualPath
        {
          get
          {
            return (bool)ViewState["useVirtualPath"];
          }
    
          set
          {
            ViewState["useVirtualPath"] = value;
          }
        }
    
        [Bindable(true), Category("Content"), DefaultValue("false")]
        public string CssClass
        {
          get
          {
            return (string)ViewState["CssClass"];
          }
    
          set
          {
            ViewState["CssClass"] = value;
          }
        }
    
        protected override void Render(HtmlTextWriter writer)
        {
          if (string.IsNullOrEmpty(Url) && UsePageVirtualPath == false)
          {
            _href = CreateUri(HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority), HttpContext.Current.Request.ApplicationPath).ToString();
          }
          else if (!string.IsNullOrEmpty(Url) && UsePageVirtualPath == true)
          {
            _href = CreateUri(HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority), CombineUri(VirtualPath, Url)).ToString();
          }
          else
          {
            _href = CreateUri(HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority), CombineUri(HttpContext.Current.Request.ApplicationPath, Url)).ToString();
          }
    
          writer.WriteBeginTag("a");
          writer.WriteAttribute("href", _href);
          writer.WriteAttribute("title", Title);
          writer.WriteAttribute("class", CssClass);
          writer.Write(HtmlTextWriter.TagRightChar);
          writer.Write(LinkText);
          writer.WriteEndTag("a");
    
          base.Render(writer);
        }
    
        private Uri CreateUri(string baseUri, string relativeUri)
        {
          Uri result = null;
    
          if (Uri.TryCreate(new Uri(baseUri), relativeUri, out result))
          {
            return result;
          }
    
          return result;
        }
    
        private string CombineUri(string basePath1, string basePath2)
        {
          return string.Format("{0}/{1}", basePath1.TrimEnd('/'), basePath2.TrimStart('/')); 
        }
      }
    }
    
        2
  •  1
  •   Tom cmoron    15 年前

    我现在为一些项目所做的方法是创建一个rootpath变量,该变量包含正确数量的 ../ 是为了找到根。像这样的……

     $dirs        = substr_count($url, '/');        // Counts slashes in url
    
     $sRoot       = '';     // Set sRoot var
     if ( $dirs   == 0 ) {      // If they're no slashes    
       $sRoot     = './';       // Set sRoot var 
     } else {
        $x = 0;     
        while ($x < $dirs ) {       // Else, for every slash, put a ../         
          $sRoot  .= '../';
          $x++;     // Increment x var  
        }
     }
    

    这对我们来说非常有效,因为我们的htaccess文件的设置方式。不过,要让它与标准页面一起工作并不需要太多的麻烦。

        3
  •  0
  •   Craig Anderson    15 年前

    我个人总是使用绝对路径(即 /path/to/file.css )避开 base 总而言之。

    你考虑过编辑 hosts 将IP地址映射到本地主机名的文件?

    ##
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1        localhost
    172.16.185.128   some.dev.server # maps http://some.dev.server/ to http://172.16.185.128/
    
        4
  •  0
  •   Richard H    15 年前

    就我个人而言,我不喜欢使用BASE,因为它会导致您遇到的问题。绝对路径总是最好的,因为它们是明确的。如果您想说交换主题或javascript库版本,或者您的应用程序可以动态生成链接以反映这一点,例如:

    /libs/v1/javascript.js
    
    /themes/blue_theme/mycss.css