如何在 asp.net 2 中创建友好的 url
我尝试使用 IHttpModule 并成功地转换了 url, 但我的所有图像都返回路径错误(全部通过新的 url 目录)。
解决办法是什么?
I tried using the IHttpModule and managed to convert the urls just fine,
but all of my images returned path error (all going through the new url directory).
whats the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要确保在图像上使用“~/”路径符号,并确保它们都是带有 runat='server' 的服务器控件。 否则图像 url 将不会被重写。
例如,如果您有一个页面从
/Item/Bicycle.aspx
重写为
/Item.aspx?id=1234
那么将会发生这样的图像引用:
将被破坏。 因此,您必须执行以下操作:
或者您可以对图像使用绝对路径。 或者您可以将尽可能多的内容推送到 .css 文件中。
You need to make sure that you use the "~/" path notation on your images and make sure that they are all server controls with runat='server'. Otherwise the images urls won't get rewritten.
For example if you have a page that gets rewritten from:
/Item/Bicycle.aspx
to
/Item.aspx?id=1234
Then what will happen is that an image reference like this:
will break. So instead you have to do something like this:
Alternatively you can use absolute paths for your images. Or you can push as much as possible into your .css files.
您可以尝试使用 URL 重写器,例如 IIRF。
使用 IIRF,您可以根据需要使用正则表达式来解析传入的 URL,然后将其发送到正确的位置。
他们在 IIRF 下载中内置了有关如何执行所有操作的示例。
You can try using a URL rewriter such as IIRF.
With IIRF you can use regular expressions to parse the incoming URL as you wish, then send it to the right place.
They have examples built in on how to do all that in the IIRF download.
解决办法是什么? 使用 .NET 3.5 中的新路由引擎,该引擎在 MVC 项目中启动并提升到独立状态。 :)
如果 Keltex 的建议不能解决您的具体问题,请查看 ResolveUrl 和 ResolveClientUrl。
What's the solution? Use the new routing engine in .NET 3.5 that started in the MVC project and got elevated to stand-alone status. :)
If Keltex's suggestion doesn't solve your specific problem look at ResolveUrl and ResolveClientUrl.