简体   繁体   English

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

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

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.这在 Cassini 中运行良好,但是当我将应用程序部署到 IIS(虚拟目录)时,IIS 找不到路径。 The virtual path is being ignored.虚拟路径被忽略。 This also doesn't work:这也不起作用:

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

尝试这个:

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

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?有谁能解释一下吗?

You could use @Url.Content method to convert virtual relative path to absolute application path like this:您可以使用@Url.Content方法将虚拟相对路径转换为绝对应用程序路径,如下所示:

<img src=@Url.Content("~/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">

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 管理器中,找到您的目录,右键单击它,然后“转换为应用程序”。

I know this is an old post but the information I needed was here.我知道这是一篇旧帖子,但我需要的信息在这里。

For me the missing part was "When Published".对我来说,缺少的部分是“发布时”。 So for any future developers looking for how to work with web config transformations, the process I follow is:因此,对于任何未来寻求如何使用 Web 配置转换的开发人员,我遵循的过程是:

  1. Add a transformation file for each project configuration (ie deployment)为每个项目配置(即部署)添加一个转换文件

  2. Add the environment specific elements (don't forget the xdt attributes)添加环境特定元素(不要忘记 xdt 属性)

  3. Modify the Web.config for your local development.为您的本地开发修改 Web.config。

When the project is published, the transformation will occur.项目发布后,将发生转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM