Html 如何在MVC中定义img src路径
我有一个带有图像的index.cshtmlHtml 如何在MVC中定义img src路径,html,asp.net-mvc,Html,Asp.net Mvc,我有一个带有图像的index.cshtml 以及myApp/Images文件夹中的一个文件夹Images 如果我访问 http:/localhost/myApp或 http:/localhost/myApp/home/ 工作正常。但是如果我使用 http:/localhost/myApp/home/index 页面尝试在中查找图像 http:/localhost/myApp/home/Images/send.png 那么我应该如何定义src 传统注释: 除了index.cshtml
myApp/Images
文件夹中的一个文件夹Images
如果我访问
- http:/localhost/myApp或
- http:/localhost/myApp/home/
- http:/localhost/myApp/home/index
- http:/localhost/myApp/home/Images/send.png
$('#u3430_img').hover(
function () {
$(this).attr("src", "~/Images/send_hover.png");
},
function () {
$(this).attr("src", "Images/send.png");
}
);
您可以使用
~
引用项目顶层中的图像
<img src="~/Images/send.png">
在Cshtml页面中,添加~是不必要的。只需添加/Images/send.png…它将进入根目录并遵循层次结构。但是在Javascript中呢。我试图通过JQuey更改src,得到
http://localhost/myApp/~/Images/send.png
你能发布你的jQuery代码吗?通常,如果我在css文件或其他文件中指定文件,我会使用相对路径(例如:../images/send.png),如果它是~/Content中的文件或使用Javascript修复程序更新的文件,我想我必须选择选项1。因为如果脚本位于单独的js文件中,选项2似乎不起作用。无法识别Razor代码。我认为选项1更好,因为您没有将其绑定到Razor。我对MVC路径分辨率的理解是,通常只需尝试/、~/、和../的变化,直到它工作为止。
$('#image').attr('src', '/Images/send.jpg');
$('#image').attr('src', '@Url.Content("~/Images/send.png")');