各个域上的 HTML 相关链接

发布于 2024-09-02 05:36:08 字数 567 浏览 4 评论 0原文

我有一个快速说明:

当您编码/开发主题时,如何链接到 html/css 代码中的各种文件?

例子: 我们公司主要在主模板中使用 ,然后只使用

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

更新页面时也会发生同样的事情 - 在开发服务器上,我们必须编辑基本目标,因此浏览网站不会将我们带到实时网站 - 这部分实际上相当简单 PHP (if ... echo else echo some else),但它仍然没有解决更多编码测试问题。

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

I have quickie:

When you code/develop themes, how do you link to various files in your html/css code?

Example:
We at our firm use mostly <base target="http://whatever"> in our main template and then just <img src="./images/file.png"> in our html, "/category/page" as links and something alike in our css.

However, when testing on different machines, we use ip address rather than localhost on main dev station of coder, so all base links don't work (because localhost goes to viewing machine, not coder's, in our network).

Same thing happens when updating pages - on dev server, we have to edit base target, so browsing site won't take us to live site - this part is actually rather simple PHP (if ... echo else echo something else), but it still not solve problem of more coding-testing problems.

So, my question is, how do YOU solve it? How do you use relative links, which basically don't care for what domain is the page on and don't care for url rewrite? (because ../images/ is different for / and different for /something/somethingElse/page)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后来的我们 2024-09-09 05:36:08

我很熟悉你上面描述的问题。当您有多个环境(开发、测试、生产等)时,这是一个特殊的问题......我已经尝试了上面每个人建议的大部分内容,但发现它们仅部分有效。为了最终解决这个问题,我所做的就是创建一个自定义控件,我称之为“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('/')); 
    }
  }
}

I am familar with the issue you describe above. This is a particular issue when you have several environments, Development, Test, Production, etc... I have tried most of what everyone has suggested above, but have found them to be only partially effective. What I did to finally solve this issue was to create a custom control that I call an "AnchorDomain". This control simply allows you to put in a path such as "/category/page" or "/images/file.png" into its VirtulPath propery and it will dynamically build the correct url for you based on the environment in which you are running your project. This way for all the problem url in my project I simply use this control and am able to run my code independent of the environment and all of my url resolve with the correct name. So for instance here is an example of the syntax in a aspx page.

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

So in this case no matter what environment you are in this link will always show the correct address.

At first it might seem like overkill to do it this way, but this control solves this issue in all cases i have run across.

See code below:

Enjoy!

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('/')); 
    }
  }
}
ま柒月 2024-09-09 05:36:08

我现在在几个项目中采用的方法是创建一个 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 文件的设置方式。不过,不需要太多的摆弄就可以让它与标准页面一起工作。

The way I've done it for a few projects now is to create a rootPath variable that contains the right amount of ../'s to get to the root. Something like this....

 $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  
    }
 }

This works perfectly for us because of the way our htaccess file is set up. It wouldn't take much fiddling to get it to work with standard pages though.

歌入人心 2024-09-09 05:36:08

就我个人而言,我总是使用绝对路径(即/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/

Personally I always use absolute paths (i.e. /path/to/file.css) and steer clear of base altogether.

Have you considered editing the hosts file to map IP addresses to host names locally?

##
# 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/
慈悲佛祖 2024-09-09 05:36:08

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

/libs/v1/javascript.js

/themes/blue_theme/mycss.css

Personally I dislike the use of base as it leads to the sort of issues you are experiencing. Absolute paths are always best as they are unambiguous. If you want to say switch themes or javascript library versions or whatever your links could be dynamically genererated by your application to reflect this, e.g:

/libs/v1/javascript.js

/themes/blue_theme/mycss.css
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文