我正在使用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...
对于图像src使用@ Url.Content将是非常好的,但是我收到一个不可用的错误。有什么我可以添加或更改使用这个?一旦该应用程序在服务器上,我宁可不需要纠正路径和出现问题。
谢谢!
解决方法
看起来有人比我更好地问这个问题。看这个:
In ASP.NET MVC how can use I Url.Content() from code?
您也可以在MVC Helper中使用@Href,如下所示:
src="@Href("../../Images/trend_up.png")" (whatever path is relative to the cshtml file) -- or -- src="@Href("~/Images/trend_up.png")"
以上是MVC3。
在MVC4中,你会得到这个很好的快捷方式(注意〜):
<img id="img1" src="~/Images/trend_up.png" />
感谢Rick Anderson,Jon galloway和Eilon Lipton的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。