具有虚拟目录的 ASP.net 母版页中的基本 URL
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这些似乎是您正在使用的绝对 URL,而不是相对 URL,这可能就是
标记没有的原因想要的效果:-- 来自 http://www.w3.org/TR/ html401/struct/links.html#h-12.4
您可以尝试从您的网址中删除前导的“/”,看看是否有效?
如果做不到这一点,我倾向于使用 ResolveClientUrl 要解决此类问题,您可以按照其他人建议的使用 ResolveUrl 的方式使用它:
希望这会有所帮助。
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:-- 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:
Hope this helps.
如果设置了 *'runat="server"' 属性,大多数标签(包括常规 HTML 标签,例如、 等)都可以使用 ~/ 作为应用程序根路径。
例如。
这使得标记成为服务器标记,并且在输出返回到浏览器之前波浪号被应用程序根替换。
对于
要解决此问题,您可以使用 注册客户端脚本 方法之一。脚本标记中的 <%= 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.
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.
此静态方法返回您应用程序根文件夹(网站或虚拟目录)的完整http路径,
因此,您可以在母版页中编写:
This static method returns you full http path to root folder of your application (web site or virtual directory)
So, you can write in master page:
在你的参考中使用波形符(~)(即~/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.
所以我昨晚发现了 IIS 的怪异之处:
在主 IIS 站点的子目录中的虚拟应用程序中无法正常工作。
相反,您必须这样做:
这是执行此操作的标准方法,但如果您不寻找它,您可能会惊讶地发现附加标记使相对路径问题消失。
So I found this IIS weirdness last night:
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:
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.