无法在 .NET Web 应用程序中找到图像

发布于 2024-12-28 00:22:40 字数 548 浏览 3 评论 0原文

我将页面的一些 html 内容保存到数据库中。根据情况,我从数据库中获取内容的html并将其放入页面。

示例内容:

<p>
Test Content
<img src="~/images/test.jpg" />
</p> 

问题是~没有按我的预期工作。如何配置 ~ 作为 Web 应用程序任何文件的绝对路径,例如 .css、.aspx 中的文件,以便将其呈现为 www.test.com/images/test .jpg ?

根据答案:

我有一个内容位于数据库中,因此即使我添加它们 runat=server,图像' src 或锚点' href 也会呈现为 html 内容。所以我应该做的是,在将字符串放入 aspx 页面之前,我应该运行一个函数并将此内容作为参数提供给它。然后将网站的绝对路径放在任何 src 或 href 标签中,其中包含 ~ 。这可以通过正则表达式来完成。

I keep some html content of the pages to the database. According to situation I get the html of content from the database and put it to the page.

Example content:

<p>
Test Content
<img src="~/images/test.jpg" />
</p> 

The problem is ~ doesnt work as I expected. How do I configure ~ to work as an absolute path of the web application any of files e.g. in .css, .aspx so it will be rendered as www.test.com/images/test.jpg?

According to answers:

I have a content that is on the database so the image' src or anchor' href is rendered just as an html content even if I add them runat=server. So what I should do is, before putting string to the aspx page, I should run a function and give this content as a parameter to it. Then put the absolute path of website any src or href tags where a ~ in it. This can be done by regex.

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

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

发布评论

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

评论(3

青衫儰鉨ミ守葔 2025-01-04 00:22:40

对于 CSS 内容链接,您可以遵循下面的相对路径 HTML 指南,

 ../filename  - links up one directory
 ../../filename - filename is located two directories up
 /filename - domain name root
 filename - file is in current directory
 folder/filename - file is located in folder sub-folder

对于 .aspx 页面中的 HTML,您可以使用,

 //WebForms
 src="<%#VirtualPathUtility.ToAbsolute("~/filename")%>"

 //MVC 
 src="<%=Url.Content("~/filename")%>"

For CSS content linking you can follow relative path HTML guidance below,

 ../filename  - links up one directory
 ../../filename - filename is located two directories up
 /filename - domain name root
 filename - file is in current directory
 folder/filename - file is located in folder sub-folder

For HTML in .aspx pages you can use,

 //WebForms
 src="<%#VirtualPathUtility.ToAbsolute("~/filename")%>"

 //MVC 
 src="<%=Url.Content("~/filename")%>"
娇纵 2025-01-04 00:22:40

只需删除“~”,您始终可以使用“/”指向网站的根目录,

所以只需

<p>
Test Content
<img src="/images/test.jpg" />
</p> 

just remove the "~" you can always point to the root of the website by using "/"

So just

<p>
Test Content
<img src="/images/test.jpg" />
</p> 
对不⑦ 2025-01-04 00:22:40

请添加

runat=服务器

-img 的属性

Please add the

runat=server

-attribute to the img

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