0

我在 ASP.NET 4 下使用 URL 重写(使用 ISAPI_Rewrite),我发现我的一些图像没有加载,因为 .NET 似乎不理解我使用的是 html BASE 标记(非常标准且必不可少在进行 URL 重写时):

例如,在我的开发环境中,我有:

<base href='http://localhost/venuefinder/Website/'></base>

在我的页面上,我有:

<asp:ImageButton runat="server" ImageUrl="~/images/button.gif" />

在网站的主页(http://localhost/venuefinder/Website/)上这工作正常,但是在使用 URL 重写的页面上,图像不起作用:

/venuefinder/Website/venues/ashton_gate_stadium/V18639/

..因为浏览器正在尝试加载:

http://localhost/images/buttons/search-button.gif

代替:

http://localhost/venuefinder/Website/venues/images/buttons/search-button.gif

发生这种情况是因为 .NET 将按钮呈现为:

src="../../../images/buttons/search-button.gif"

...这是不正确的。

有什么办法可以纠正这个问题,以便 .NET 为图像呈现正确的 src 属性?(没有所有 ../../../ 等)

4

1 回答 1

2

不要~/在图像中使用,而是在 IIS 中创建一个名为的虚拟文件夹,该文件夹images映射到您的图像目录(因此:http://localhost/images=> c:\myproject\somesubdir\content\images)。然后你可以使用

<asp:ImageButton runat="server" ImageUrl="/images/button.gif" />

我们为所有网站内容项(脚本/图像/css)执行此操作。

如果您<base>在 HTML 中使用,浏览器会将给定的 url 作为以 . 开头的图像的基础/。所以在你的情况下,你可以放弃波浪号。


然而,虚拟目录方法的优点之一是您再也不必担心 url 结构,无论是在 .NET 还是 JS 中:

ScriptHelper.RegisterScript("/js/somefile.js")

或者

$().append('<img src="/images/file.jpg"/>')
于 2010-04-23T11:09:49.987 回答