MVC帮手-使用@URL进行图片src吗? [英] MVC helper - using @URL for image src?

查看:200
本文介绍了MVC帮手-使用@URL进行图片src吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

@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...

将@ Url.Content用作图像src会很棒,但出现错误,提示它不可用.我可以添加或更改使用它吗?该应用程序在服务器上后,我宁愿不需要弄乱路径并遇到问题.

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.

谢谢!

推荐答案

好像有人问这个问题比我要好.看这个:

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

在ASP.NET MVC中如何从C#代码中使用Razor @ Url.Content()帮助器?

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

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

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

以上是针对MVC3的.

The above are for MVC3.

在MVC4中,您会获得一个不错的快捷方式(请注意〜):

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

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

感谢里克·安德森(Rick Anderson),乔恩·加洛韦(Jon Galloway)和埃隆·利普顿(Eilon Lipton)的帮助.

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

这篇关于MVC帮手-使用@URL进行图片src吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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