Url.Content()不工作的复杂的URL [英] Url.Content() not working on complex URLs

查看:182
本文介绍了Url.Content()不工作的复杂的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Url.Content()在我的观点随处可见片段。他们正常工作与一个简单的URL的网页,而不是当URL变长。

I have Url.Content() snippets everywhere in my views. They work fine on pages with a simple URL, but not when the URL gets longer.

下面是一个例子:

<img src="@Url.Content("~/Content/Images/logo.png")" id="logo">

这工作得很好,当我在主页上,或网址页面本地主机:8080 / S /巧克力(其显示结果为巧克力搜索

This works fine when I'm on the homepage, or a page with URL localhost:8080/s/chocolate (which shows the result for the "chocolate" search.

但是,当我尝试添加一些改进,例如本地主机:8080 / S /巧克力/ B /瑞士莲(即过滤previous业绩与品牌瑞士莲唯一的),它不工作了。在这种情况下, Url.Content /s/chocolate/Content/Images/logo.png ,这明显失败。

But when I'm trying to add some refinements, e.g. localhost:8080/s/chocolate/b/lindt (which means filter the previous results to only ones from brand Lindt), it doesn't work anymore. In this case, Url.Content points to /s/chocolate/Content/Images/logo.png, which obviously fails.

这是因为如果 Url.Content 只到达2级了目前的位置,而不是使用Web应用程序的真正根源。我想这是有道理的惯例,网址形式的主机/控制器/动作的,但在这里我有一个更复杂的URL方案(我用的URL重写模块来匹配这些URL片段操作的参数)。

It's as if Url.Content only goes 2 levels up the current location instead of using the real root of the web app. I guess it makes sense in the convention that URLs are in the form host/controller/action, but here I have a more complex URL scheme (I use URL rewriter module to match these URL fragments to action's parameters).

有没有办法告诉助手去真正的根,或任何其他办法解决这个问题?

Is there any way to tell the helper to go to the real root, or any other solution to this problem?

(顺便说一句,我使用MVC 4)

(BTW, I'm using MVC 4)



修改
正如菲利普回答,我刚刚发现Url.Content不再需要用MVC 4,对于具有恒定的路径中的所有外观设计的图像作品。但是,我用大量的图片,其中的一些数据部分构成的路径,例如


As Felipe answered, I've just discovered that Url.Content is no longer necessary with MVC 4. That works for all "design" images with a constant path. However, I use a lot of images where the path is constructed partly with some data, e.g.

<img src="@Url.Content(string.Format("~/Content/Images/stores/{0}.png", cart.Store.Retailer.Id))"/>

我只是删除了Url.content,因为这样的:

I simply removed the Url.content, as such:

<img src="@string.Format("~/Content/Images/stores/{0}.png", Model.PreferedCart.Store.Retailer.Id)"/>

当渲染,这给下面的src:〜/内容/图片/ stores_v2 / Fr_SimplyMarket.png 。在是还在这里,图像没有找到。我该如何解决呢?

When rendered, this gives the following src: ~/Content/Images/stores_v2/Fr_SimplyMarket.png. The ~ being still here, the image is not found. How can I fix that?

推荐答案

在ASP.NET MVC 4+,你并不需要使用 Url.Content 属性值。直接使用〜/ 语法和让的剃刀视图引擎2 处理你的路径。所以,你的例子可以codeD如下:

In ASP.NET MVC 4+, you do not need to use Url.Content in attribute values. Use the ~/ syntax directly and let razor view engine 2 process the path for you. So your examples can be coded as follows:

<img src="~/Content/Images/logo.png" id="logo">

在这种情况下,你需要创建一个动态路径,所以,你必须使用 Url.Content ,样品:

@{
   string imagePath = Url.Content("~/Content/Images/stores/" + Model.PreferedCart.Store.Retailer.Id + ".png");
}
<img src="@string"/>

如果它不工作,原因是因为你有你的URL重写的问题。

If it does not work, the reason is because you have problems with your URL rewriter.

这篇关于Url.Content()不工作的复杂的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆