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

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

ASP.NET MVC images and other static content url

asp.net-mvcasp.net-mvc-routing

提问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/ShawnMcleanImages 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/1then everything loads fine.

现在,当我的 url 看起来像这样时:localhost/Users/Details/1/ShawnMclean图像不会从控制器和 site.master 加载。(不知道为什么 css 和 javascript 有正确的网址)。如果 url 是localhost/Users/Details/1那么一切加载正常。

My img in site.masterand Details.aspxlooks like this in the old url:

我在IMGsite.masterDetails.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>