具有虚拟目录的 ASP.net 母版页中的基本 URL

发布于 2024-08-24 21:57:21 字数 547 浏览 6 评论 0原文

我有一个 ASP.net 母版页。在这个 master 中,我定义了所有 css 和 javascript 文件。我还有一些图像、一些按钮和超链接。

所有网址均声明为相对网址,即“/scripts/ian.js”

如果该网站是根网站,则一切正常,但我需要它在虚拟目录中工作。

我的问题是,当我将此网站放置在根站点下的虚拟目录中时,我的所有链接都指向根站点。所以我的链接指向 www.root.com/scripts/ian.js 但它应该指向 www.root.com/virtualDir/scripts/ian.js

我认为 Base Href 标头中的标记会有所帮助,但到目前为止它似乎没有任何帮助。当我将鼠标悬停在所有链接上时,它们仍然指向根网站。

我想要的是在 IIS 或配置文件中进行单个设置,我可以设置根 url,并且母版页或内容页上的任何图像、脚本或链接都将指向正确的位置。

欢迎任何建议或想法。

谢谢

I have an ASP.net master page. In this master, I have all my css and javascript files defined. I also have a few images and a few buttons and hyperlinks.

All the urls are all declared as relative ie "/scripts/ian.js"

Everything works fine if this site is the root website, but I need it to work in a virtual directory.

My problem is when I place this website in a virtual directory under a root site, all my links are pointing to the root site. so my links point to www.root.com/scripts/ian.js but it should be pointing to www.root.com/virtualDir/scripts/ian.js

I thought the Base Href tag in the header would help, but so far it does not seem to be helping in anyway. All the links are still pointing to the root website when i hover over them.

What I would like is a single setting either in IIS or the config file that I can set a root url and any image, script or link either on the master page or content page, would point to the right place.

Any suggestions or ideas are welcome.

Thanks

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

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

发布评论

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

评论(5

℉絮湮 2024-08-31 21:57:21

所有网址均声明为
相对即“/scripts/ian.js”

这些似乎是您正在使用的绝对 URL,而不是相对 URL,这可能就是 标记没有的原因想要的效果:

该属性指定一个绝对值
充当基本 URI 的 URI
解析相对 URI。

-- 来自 http://www.w3.org/TR/ html401/struct/links.html#h-12.4

您可以尝试从您的网址中删除前导的“/”,看看是否有效?

如果做不到这一点,我倾向于使用 ResolveClientUrl 要解决此类问题,您可以按照其他人建议的使用 ResolveUrl 的方式使用它:

<script type="text/javascript" src="<%= ResolveClientUrl("~/path/to/js") %>"></script>
...
<img src="<%= ResolveClientUrl("~/path/to/img") %>" alt="..." />

希望这会有所帮助。

All the urls are all declared as
relative ie "/scripts/ian.js"

Those seem to be absolute URL's that you're using, rather than relative URL's, which is probably why the <base /> tag isn't having the desired effect:

This attribute specifies an absolute
URI that acts as the base URI for
resolving relative URIs.

-- from http://www.w3.org/TR/html401/struct/links.html#h-12.4

You could try removing the leading '/' from your URL's to see if that works?

Failing that, I tend to use ResolveClientUrl to get around issues like this, which you'd use in the same way as others have suggested using ResolveUrl:

<script type="text/javascript" src="<%= ResolveClientUrl("~/path/to/js") %>"></script>
...
<img src="<%= ResolveClientUrl("~/path/to/img") %>" alt="..." />

Hope this helps.

眉黛浅 2024-08-31 21:57:21

如果设置了 *'runat="server"' 属性,大多数标签(包括常规 HTML 标签,例如 等)都可以使用 ~/ 作为应用程序根路径。

例如。

<img src="~/images/test.png" runat="server" />

这使得标记成为服务器标记,并且在输出返回到浏览器之前波浪号被应用程序根替换。

对于

要解决此问题,您可以使用 注册客户端脚本 方法之一。脚本标记中的 <%= ResolveUrl('~')%> 标记。

Most tags, including regular HTML tags like <link>, <img>, etc can use the ~/ as the application root path if the *'runat="server"' attribute is set.

eg.

<img src="~/images/test.png" runat="server" />

This makes tag a server tag and the tilde is replaced with the application root before the output is returned to the browser.

This doesn't work as expected for the <script> though. When 'runat="server' is set for the script tag, then the script is considered to be server-side javascript and execution is attempted.

To work around this you can either inject the javascript using one of the register client script methods. your you can use the <%= ResolveUrl('~')%> tag in your script tag.

我是有多爱你 2024-08-31 21:57:21

此静态方法返回您应用程序根文件夹(网站或虚拟目录)的完整http路径,

public static string GetAppRootUrl(bool endSlash)
{
    string host = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);

    string appRootUrl = HttpContext.Current.Request.ApplicationPath;
    if (!appRootUrl.EndsWith("/")) //a virtual
    {
        appRootUrl += "/";
    }
    if (!endSlash)
    {
        appRootUrl = appRootUrl.Substring(0, appRootUrl.Length - 1);
    }
    return host + appRootUrl;
}

因此,您可以在母版页中编写:

<script src="<%= Server.HtmlEncode(GetAppRootUrl(false)) %>/scripts/ian.js" language="javascript" type="text/javascript"></script>

This static method returns you full http path to root folder of your application (web site or virtual directory)

public static string GetAppRootUrl(bool endSlash)
{
    string host = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);

    string appRootUrl = HttpContext.Current.Request.ApplicationPath;
    if (!appRootUrl.EndsWith("/")) //a virtual
    {
        appRootUrl += "/";
    }
    if (!endSlash)
    {
        appRootUrl = appRootUrl.Substring(0, appRootUrl.Length - 1);
    }
    return host + appRootUrl;
}

So, you can write in master page:

<script src="<%= Server.HtmlEncode(GetAppRootUrl(false)) %>/scripts/ian.js" language="javascript" type="text/javascript"></script>
羁绊已千年 2024-08-31 21:57:21

在你的参考中使用波形符(~)(即~/scrips/ian.js)...看看它是否有效
对于链接,请尝试 .aspx 页面中的 Page.ResolveUrl。

Use tilde(~) in yout reference (i.e ~/scrips/ian.js)...see if it works
For links try Page.ResolveUrl in the .aspx page.

潦草背影 2024-08-31 21:57:21

所以我昨晚发现了 IIS 的怪异之处:

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

在主 IIS 站点的子目录中的虚拟应用程序中无法正常工作。

相反,您必须这样做:

<script src="/js/file.js" type="text/javascript"></script>

这是执行此操作的标准方法,但如果您不寻找它,您可能会惊讶地发现附加标记使相对路径问题消失。

So I found this IIS weirdness last night:

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

Will not work properly in a virtual application that's in a subdirectory of the main IIS site.

Instead, you MUST do it like this:

<script src="/js/file.js" type="text/javascript"></script>

Which is the standard way to do it, but if you're not looking for it, it can surprise you that that additional tag makes the relative path issues go away.

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