asp.net-mvc ASP.NET MVC 图像和其他静态内容 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4001168/
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
ASP.NET MVC images and other static content url
提问by Shawn Mclean
I just edited my route for a user details page to look like this:
我刚刚编辑了用户详细信息页面的路线,如下所示:
routes.MapRoute(
"UserDetails", // Route name
"{controller}/{action}/{id}/{title}", // URL with parameters
new { controller = "Users", action = "Details", id = UrlParameter.Optional, title = UrlParameter.Optional } // Parameter defaults
);
Now when my url looks like this: localhost/Users/Details/1/ShawnMclean
Images do not load both from the controller and the site.master. (no idea why the css and javascript had correct urls though). If the url is localhost/Users/Details/1
then everything loads fine.
现在,当我的 url 看起来像这样时:localhost/Users/Details/1/ShawnMclean
图像不会从控制器和 site.master 加载。(不知道为什么 css 和 javascript 有正确的网址)。如果 url 是localhost/Users/Details/1
那么一切加载正常。
My img in site.master
and Details.aspx
looks like this in the old url:
我在IMGsite.master
和Details.aspx
看起来像这样在旧的URL:
<img src="../../Content/Images/logo3.png" />
but when the url gets an additional parameter, the image is actually located at ../../../Content/Images/logo3.png
但是当url得到一个额外的参数时,图像实际上位于 ../../../Content/Images/logo3.png
Is there a way to make images and other static content's url change?
有没有办法让图片和其他静态内容的 url 改变?
回答by quakkels
Try linking your images like this:
尝试像这样链接您的图像:
<img src="/Content/Images/logo3.png" />
or if that doesn't work you could always use a helper for your links
或者,如果这不起作用,您可以随时使用帮助程序作为链接
<img src="<%= Url.Content("~/Content/Images/logo3.png") %>" />
one other way could be
另一种方式可能是
<img src="@Url.Content("~/Content/Images/logo3.png")" />
回答by Justin Soliz
You can try using a helper:
您可以尝试使用助手:
<img src='<%= Url.Content( "~/Content/Images/pic.jpg" ) %>' alt="My Image" />
回答by Duk
You can try this,
你可以试试这个
<a href="/"><img src="<%=Url.Content("~/Content/Images/logo.png")%>" alt="logo" title="Logo" /></a>