解决MVC中的虚拟路径
嗨,
我收到了这样的地址:~/Content/Files/AdImages/20/20_thumb.jpeg,我需要解决这个问题。这是在 ASP.net 中使用 Control.ResolveUrl() 完成的。
根据这篇文章
urlHelper.Content("~/Content/Files/AdImages/20/20_thumb.jpeg")
但这确实返回以下网址:/Content/Files/AdImages/20/20_thumb。 jpeg 即使我不在解决方案的根目录中(内容放置在根目录中)?
我该如何解决这个问题?
请注意,这是来自 html 帮助程序扩展。
//雪吉姆
Hi,
I got a adress like this : ~/Content/Files/AdImages/20/20_thumb.jpeg, I need this to be resolved. This was done in ASP.net with Control.ResolveUrl().
According to this article http://stephenwalther.com/blog/archive/2009/02/18/asp.net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx, I should use somthing like this :
urlHelper.Content("~/Content/Files/AdImages/20/20_thumb.jpeg")
This does however returns the following url : /Content/Files/AdImages/20/20_thumb.jpeg even when Im not in the root of the solution(Content is placed in the root)?
How can I resolve this?
note that this is from within a html helper extension.
//SnowJim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
~ 的工作是始终解析到根,以便您看到正确的行为。如果您希望它相对于您当前所在的路径,那么您的路径不应以 ~ 或 / 开头
The job of the ~ is to always resolve to the root so you are seeing the correct behavior. If you want this to be relative to the path where you currently are then your path should not start with a ~ or a /
是的,〜在 asp.net 中的工作方式与在 mvc 中的工作方式不同。相反,最好的选择似乎是将 ~ 替换为:
Request.Url.GetLeftPart(UriPartial.Authority)
因此,不要
使用:
Yeah, ~ doesn't work the same in asp.net as in mvc. Instead, the best option seems to replace the ~ with:
Request.Url.GetLeftPart(UriPartial.Authority)
So instead of:
use