RouteHandler 与相对路径冲突

发布于 2024-10-31 10:58:34 字数 845 浏览 5 评论 0原文

我的项目(某些东西)中有一个与此类似的目录结构:

Root
  |
  |- Utilities
  |
  |- Modules
      |
      |- Admin

在 admin 目录中,我有一个简单的页面 default.aspx 和一个 javascript 文件 validation.js

我只是想在我的 aspx 页面中引用这个文件,

<script src="validation.js" ...></script>

因为我正在使用路由处理程序,我的 url 将像这个模板一样

http://mysubdomain.mysite.com/admin/UserId/manage

(用三重 des 加密字符串替换 userid 并设置),

所以因为正在使用相对路径要访问js文件,当我尝试签入firebug时,它尝试使用此路径访问js文件:

http://mysubdomain.mysite.com/admin/UserId/manage/validation.js

当然,它不存在。我看到的唯一方法是

src="\modules\admin\validation.js"

肯定指定完整的相对路径,应该有某种方法可以在没有完整相对路径的情况下使用此验证js。如何得到它?

谢谢。

ps:这全部在 .aspx 页面中,而不是代码隐藏 (.cs)

I have a directory structure in my project (something) similar to this one:

Root
  |
  |- Utilities
  |
  |- Modules
      |
      |- Admin

in the admin directory, I have a simple page default.aspx and a javascript file validation.js

I am just trying to refer to this file in my aspx page like

<script src="validation.js" ...></script>

since I am using route handlers, my url will be like this template

http://mysubdomain.mysite.com/admin/UserId/manage

(replace userid with a triple des encrypted string and you are set)

so since the relative path is being used to access the js file, when I try to check in firebug, its trying to access the js file with this path:

http://mysubdomain.mysite.com/admin/UserId/manage/validation.js

of course, its not present. the only way I see is to specify full relative path

src="\modules\admin\validation.js"

surely, there should be some way I can use this validation js without the full relative path. How to get that?

thanks.

ps: this is all in .aspx page and not codebehind (.cs)

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

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

发布评论

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

评论(2

毁虫ゝ 2024-11-07 10:58:34

我也遇到过这个问题,但始终无法采取任何措施。我通常最终使用完整的相对路径,如果目录发生变化,那么我每次都必须手动更改它。我认为没有其他方法可以做到这一点。

当然,我很乐意被证明是错误的。

I have had this problem too but never been able to do anything about it. I usually end up using a full relative path and if the directory changes, well, I have to change it manually everytime. I see no other way to do this.

Of course, I would love to be proved wrong.

回首观望 2024-11-07 10:58:34

我认为您正在寻找的是 HTML base 标签。

在任何外部引用之前的 head 元素中指定 将使引用将 http://mysubdomain.mysite.com/modules/admin/ 作为路径根。要在整个站点中使用此功能,您可以编写一个小脚本并将其包含在母版页中。

我还没有测试过,但我认为像下面这样的东西应该可以工作。 :

    public const string BASE_HREF_FORMAT = @"<base href=""http://{0}"" />";
    public static string GetBaseHref(HttpContext httpContext){
        var sv = httpContext.Request.ServerVariables;
        var sb = new StringBuilder();
        var serverName = sv["SERVER_NAME"];
        var path = sv["PATH_INFO"];
        if(path.Contains("/"))
        {
            path = path.Substring(0, path.LastIndexOf("/")+1);
        }
        return string.Format(BASE_HREF_FORMAT, path+serverName);
    } 

您可以在以下位置找到文档和教程: http://www.w3schools.com/tags/att_base_href .asp

I think what you are looking for is the HTML base tag.

Specifying <base href="http://mysubdomain.mysite.com/modules/admin/" /> in the head element prior to any external references will make the references take http://mysubdomain.mysite.com/modules/admin/ as the path root. To get this functionality throughout your site, you can write a little script and include it in your master page.

I haven't tested it, but I think something like the following should would work. :

    public const string BASE_HREF_FORMAT = @"<base href=""http://{0}"" />";
    public static string GetBaseHref(HttpContext httpContext){
        var sv = httpContext.Request.ServerVariables;
        var sb = new StringBuilder();
        var serverName = sv["SERVER_NAME"];
        var path = sv["PATH_INFO"];
        if(path.Contains("/"))
        {
            path = path.Substring(0, path.LastIndexOf("/")+1);
        }
        return string.Format(BASE_HREF_FORMAT, path+serverName);
    } 

You can find documentation and a tutorial at: http://www.w3schools.com/tags/att_base_href.asp

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