Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC图像和其他静态内容url_Asp.net Mvc_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc ASP.NET MVC图像和其他静态内容url

Asp.net mvc ASP.NET MVC图像和其他静态内容url,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我刚刚编辑了用户详细信息页面的路线,如下所示: routes.MapRoute( "UserDetails", // Route name "{controller}/{action}/{id}/{title}", // URL with parameters new { controller = "Users", action = "Details", id = UrlParameter.Optional,

我刚刚编辑了用户详细信息页面的路线,如下所示:

        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
            );
<img src="/Content/Images/logo3.png" /> 
现在,当我的url如下所示:
localhost/Users/Details/1/ShawnMclean
图像不会同时从控制器和site.master加载。(不知道为什么css和javascript有正确的URL)。如果url是
localhost/Users/Details/1
,则所有内容都可以正常加载

我在
site.master
Details.aspx
中的img在旧url中看起来像这样:

<img src="../../Content/Images/logo3.png" />

但是当url获得一个附加参数时,图像实际上位于
。/../../Content/Images/logo3.png


有没有办法更改图像和其他静态内容的url?

尝试如下链接图像:

        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
            );
<img src="/Content/Images/logo3.png" /> 

或者,如果这不起作用,你可以随时为你的链接使用一个助手

<img src="<%= Url.Content("~/Content/Images/logo3.png") %>" />
“/>
另一种可能是

<img src="@Url.Content("~/Content/Images/logo3.png")" />

您可以尝试使用助手:

<img src='<%= Url.Content( "~/Content/Images/pic.jpg" ) %>' alt="My Image" />
'alt=“我的图像”/>
您可以试试这个

<a href="/"><img src="<%=Url.Content("~/Content/Images/logo.png")%>" alt="logo" title="Logo" /></a> 


我的所有路线都有这一点。为什么
/Content/Images/logo3.png
有效?第一个斜杠是否从站点根开始?
/
表示绝对(根)。
。/
表示相对。始终尝试使用绝对路径。MVC的正确方法是使用
Url.Content(~/)
。第二个示例似乎是错误的。在
@stom之前是否真的应该有一个正斜杠:这应该是一个新问题,SO社区的人会很乐意提供帮助。您在数据库中的路径不起作用的原因可能是因为您存储的是本地路径,而不是基于http的url。当浏览器向您的网页加载以
c://
开头的img标记不知道如何访问它。请从站点根开始创建路径。因此,如果您的图像位于此处:
http://example.com/images
那么你的img标签应该以
/images
开头。你的站点中的代码是什么。master?