Html 如何在 ASP.Net MVC razor helper 中为图像指定虚拟路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9864881/
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-08-28 23:20:54  来源:igfitidea点击:

How to specify virtual path for image in ASP.Net MVC razor helper

asp.nethtmlcssasp.net-mvcrazor

提问by zszep

In my razor style helper class (located in the App_Code folder, I've got this line of code:

在我的剃刀样式助手类(位于 App_Code 文件夹中,我有这行代码:

<img src="../../Content/images/ajax_activity.gif" alt="loading"/>

This works fine in Cassini, but when I deploy the app to IIS (virtual directory), IIS can't find the path. The virtual path is being ignored. This also doesn't work:

这在 Cassini 中运行良好,但是当我将应用程序部署到 IIS(虚拟目录)时,IIS 找不到路径。虚拟路径被忽略。这也不起作用:

<img src="@Href("~/Content/images/ajax_activity.gif")" alt="loading" />

采纳答案by zszep

OK, solved it, though I'm not really sure why it's working. After trying all the following combinations without success:

好的,解决了它,尽管我不确定它为什么起作用。在尝试了以下所有组合都没有成功后:

<img src="../Content/images/ajax_activity.gif" alt="loading"/>
<img src="/Content/images/ajax_activity.gif" alt="loading"/>
<img src="~/Content/images/ajax_activity.gif" alt="loading"/>
<img src="Content/images/ajax_activity.gif" alt="loading"/>

the following finally worked as expected

以下最终按预期工作

<img src="./Content/images/ajax_activity.gif" alt="loading"/>

It returned the image path correctly with the virtual directory set. Anyone able to explain this?

它使用虚拟目录集正确返回了图像路径。有谁能解释一下吗?

回答by Charles Ouellet

Try this:

尝试这个:

<img src="@Url.Content("~/Content/images/ajax_activity.gif")" alt="loading" />

回答by almaceleste

You could use @Url.Contentmethod to convert virtual relative path to absolute application path like this:

您可以使用@Url.Content方法将虚拟相对路径转换为绝对应用程序路径,如下所示:

<img [email protected]("~/images/picture.png") alt="picture description">

It'll be converted in this HTML code forwarded to client:

它将在此 HTML 代码中转换并转发给客户端:

<img src="/appname/images/picture.png" alt="picture description">

UPDATE: In other hand you could convert image to base64 and it will be displayed correctly too:

更新:另一方面,您可以将图像转换为 base64,它也会正确显示:

<img src="data:image/png;base64,iVBORw0KG...SuQmCC" alt="picture description">

回答by glacasa

When you deploy in a virtual directory in IIS, the app root may be different from what you had in your dev environment.

当您在 IIS 中的虚拟目录中部署时,应用程序根目录可能与您在开发环境中的不同。

If your app url si something like

如果您的应用程序网址 si 类似

http://localhost/MyWebApp/

ASP.NET will consider the root is "localhost", when it should be "MyWebApp".

ASP.NET 会认为根是“localhost”,而应该是“MyWebApp”。

To solve this, you have to convert the virtual directory to an application : in IIS Manager, find your directory, right clic on it, and then "Convert to application".

要解决此问题,您必须将虚拟目录转换为应用程序:在 IIS 管理器中,找到您的目录,右键单击它,然后“转换为应用程序”。