asp.net-mvc MVC 帮助程序 - 使用 @URL 作为图像 src?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10645382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:57:10  来源:igfitidea点击:

MVC helper - using @URL for image src?

asp.net-mvcasp.net-mvc-3html-helper

提问by dex3703

I'm using MVC3 and have a simple helper that draws a box with some text and icons inside for a status application. A snippet:

我正在使用 MVC3 并有一个简单的帮助程序,它为状态应用程序绘制一个带有一些文本和图标的框。一个片段:

@helper Tile(string ID)
 {
 <div class="front defaulttile"> 
    <div class="notifybar" id="NotifyBarID" >
    <img alt="" src="@Url.Content("~/Images/img.jpg")" style="display: inline; margin-right: 5px;" />
    <p class="topstatusbartext" id="NotifyBarTextID" >Status bar text</p>
    </div>
etc...

It would be great to use @Url.Content for the image src, but I get an error it's not available. Is there something I can add or change to use this? I'd rather not need to juggle paths and having a problem once the app is on the server.

将 @Url.Content 用于图像 src 会很棒,但我收到一个错误,它不可用。有什么我可以添加或更改来使用它的吗?一旦应用程序在服务器上,我宁愿不需要处理路径和遇到问题。

Thanks!

谢谢!

回答by dex3703

Looks like somebody asked the question better than me. Look at this:

看来有人比我问得更好。看这个:

In ASP.NET MVC how can I use the Razor @Url.Content() helper from C# code?

在 ASP.NET MVC 中,如何使用来自 C# 代码的 Razor @Url.Content() 助手?

You can also use @Hrefinside an MVC Helper too, like this:

您也可以在 MVC Helper 中使用@Href,如下所示:

src="@Href("../../Images/trend_up.png")" (whatever path is relative to the cshtml file)
-- or --
src="@Href("~/Images/trend_up.png")"

The above are for MVC3.

以上是针对MVC3的。

In MVC4, you get this nice shortcut (notice the ~):

在 MVC4 中,你得到了这个不错的快捷方式(注意 ~):

<img id="img1" src="~/Images/trend_up.png" />

Thanks to Rick Anderson, Jon Galloway and Eilon Lipton for the help.

感谢 Rick Anderson、Jon Galloway 和 Eilon Lipton 的帮助。

回答by Refky Wahib

    @helper Tile(string ID,UrlHelper url)
 {
 <div class="front defaulttile"> 
    <div class="notifybar" id="NotifyBarID" >
    <img alt="" src="@url.Content("~/Images/img.jpg")" style="display: inline; margin-right: 5px;" />
    <p class="topstatusbartext" id="NotifyBarTextID" >Status bar text</p>
    </div>
etc...

回答by DB Conner

In MVC4, it's definitely fun to be able to use the tilde again, as noted in answer:

在 MVC4 中,能够再次使用波浪号绝对很有趣,如回答中所述:

src="~/Images/trend_up.png"

Just rememeber that references to those images in your CSS files will not recognize the ~- MVC4 or not.

请记住,对 CSS 文件中这些图像的引用将无法识别~- MVC4 与否。

It is not the same problem however, as the relative reference to an image in CSS url.
For example background: url('../Images/menus/menu-left.png')will be relative to the location of the CSS file. So if you can manage to have the CSS file stay put relative to other application folders, you're OK...

然而,这与 CSS url 中对图像的相对引用不同。
例如background: url('../Images/menus/menu-left.png')将相对于 CSS 文件的位置。因此,如果您可以设法让 CSS 文件相对于其他应用程序文件夹保持不变,那么您就可以了...