解决MVC中的虚拟路径

发布于 2024-09-24 05:00:47 字数 395 浏览 1 评论 0原文

嗨,

我收到了这样的地址:~/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 技术交流群。

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

发布评论

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

评论(2

凡间太子 2024-10-01 05:00:47

~ 的工作是始终解析到根,以便您看到正确的行为。如果您希望它相对于您当前所在的路径,那么您的路径不应以 ~ 或 / 开头

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 /

岁月流歌 2024-10-01 05:00:47

是的,〜在 asp.net 中的工作方式与在 mvc 中的工作方式不同。相反,最好的选择似乎是将 ~ 替换为:

Request.Url.GetLeftPart(UriPartial.Authority)

因此,不要

urlHelper.Content("~/Content/Files/AdImages/20/20_thumb.jpeg")

使用:

urlHelper.Content( Request.Url.GetLeftPart(UriPartial.Authority) + "/Content/Files/AdImages/20/20_thumb.jpeg")

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:

urlHelper.Content("~/Content/Files/AdImages/20/20_thumb.jpeg")

use

urlHelper.Content( Request.Url.GetLeftPart(UriPartial.Authority) + "/Content/Files/AdImages/20/20_thumb.jpeg")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文