本文介绍了解析相对于应用程序根目录的图像uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用Asp.Net MVC5.为此问题假设我的应用程序是从 localhost:9000/app 提供服务的.I am using Asp.Net MVC 5. Assume for this question that my application is being served from localhost:9000/app.我已经在我的 _layout.cshtml 中找到了它:I've got this in my _layout.cshtml:<img src="@Url.Content("~/Content/logo.png")" />当用户请求 localhost:9000/app/foobar 时,图像URI被正确解析为 localhost:9000/app/Content/logo.png .When the user requests localhost:9000/app/foobar, the image URI is correctly resolved to localhost:9000/app/Content/logo.png.但是,当用户请求 localhost:9000/app/folder/bender_is_great 时,图像URI解析为 localhost:9000/app/folder/Content/logo.pngBut when the user requests localhost:9000/app/folder/bender_is_great, the image URI resolves to localhost:9000/app/folder/Content/logo.png.如何获取始终相对于应用程序根而不是相对于所请求路径解析的图像URI?How do I get the image URI to always resolve relative to the application root, not relative to the requested path? Server.MapPath(〜/Content/logo.png")具有相同的问题.它是相对于请求的路径而不是应用程序根目录进行解析的. Server.MapPath("~/Content/logo.png") has the same issue. It's resolving relative to the requested path, not the application root.推荐答案原来是我的URL重写规则,用于不带井号(#!)的友好HTML5 URL.重写引擎在应用程序上设置了一个项目:"IIS_WasUrlRewrite" . Url.Content(...)调用 UrlUtil ,它会注意此项目并更改其行为. Microsoft支持页面说:Turns out it was my URL rewrite rules for friendly HTML5 URLs without the hashbang (#!). The rewriting engine set an item on the application: "IIS_WasUrlRewritten". Url.Content(...) calls into UrlUtil, which pays attention to this item and changes its behavior. This Microsoft Support page says:protected void Application_BeginRequest(object sender, EventArgs e){ Context.Items["IIS_WasUrlRewritten"] = false;}到目前为止,此代码尚未在我的应用程序中引起任何问题,并已解决了该问题.So far this code hasn't caused any problems in my application and has resolved the issue. 这篇关于解析相对于应用程序根目录的图像uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-24 17:43