图像路径和绝对路径?

发布于 2024-09-26 18:14:34 字数 616 浏览 0 评论 0原文

我正在使用 C# 在 ASP .NET MVC 2 中开发一个网站。

我有一个部分视图,Header.ascx。其中有我的网站的图像 MainImage.png

当我使用我创建的主要视图之一时,图像显示得很好。例如,我点击了新闻控制器的 Index ActionResult。 (site.com/News)

然而,当我深入挖掘时,我似乎失去了我的图像,即使部分视图是从母版页显示的。即,如果我尝试访问 site.com/News/Article/1

是否有任何建议可以保持我的图像完整无损,例如进行绝对路径的方法?

我当前在部分视图中的代码位于此处:

<div align="center"><a href="/"><img src="../Content/images/MainImage.png" style="border:none" /></a></div>

我尝试将 src 更改为 ~/Content/images/MainImage.png 但这会破坏整个网站。

I'm developing a website in ASP .NET MVC 2 using C#.

I have a partial view, Header.ascx. In there I have an image for my website, MainImage.png.

When I use one of the primary Views I've created, the image shows up fine. For instance, I hit the Index ActionResult of my News Controller. (site.com/News)

However, when I dig deeper, I seem to lose my image, even though the Partial view is being displayed from the Master page. i.e., if I try going to site.com/News/Article/1

Are there any suggestions for keeping my image fully intact, such as a way to do absolute pathing?

The code I currently have in my partial view is here:

<div align="center"><a href="/"><img src="../Content/images/MainImage.png" style="border:none" /></a></div>

I've tried changing the src to ~/Content/images/MainImage.png but that breaks it all over the site.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-10-03 18:14:34

使用 Url.Content 辅助方法。

<img src="<%: Url.Content("~/content/images/imagename.png") %>" />

当您想要包含 javascript 文件或 css 时同样适用

<link rel="stylesheet" href="<%= Url.Content("~/content/site.css") %>" type="text/css" />
<script type="text/javascript" src="<%= Url.Content("~/content/scripts.js") %>"></script>

Use the Url.Content helper method.

<img src="<%: Url.Content("~/content/images/imagename.png") %>" />

Same applies for when you want to include javascript files or css

<link rel="stylesheet" href="<%= Url.Content("~/content/site.css") %>" type="text/css" />
<script type="text/javascript" src="<%= Url.Content("~/content/scripts.js") %>"></script>
谈场末日恋爱 2024-10-03 18:14:34

只要有可能,就将资源文件(例如图像、CSS 和 JS)的路径(href 或 src)设置为相对于 Web 服务器根目录。也就是说,您的 URL 应以斜杠开头:

该格式保留当前服务器地址(可能是 IP 地址、内部网络地址或多个公共网址中的任何一个)、协议和端口,并且不依赖于用户正在查看的页面的明显路径(这可以改变,具体取决于用户是否通过以下方式访问页面)它的规范位置或通过 URL 重写)。

Whenever possible, make the path (href or src) to resource files, like images, CSS and JS relative to the web server root. That is, your URLs should begin with a slash:

<img src="/images/imagename.png" />

That format retains the current server address (which may be an IP address, an internal network address or any of a number of public web addresses), protocol and port, and doesn't depend on the apparent path of the page the user is looking at (which can change, depending on whether the user is accessing the page by its canonical location or by a URL rewrite).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文