简体   繁体   English

MVC5如何在代码中找到根路径?

[英]MVC5 how to find root path in code?

I have some helper methods to build Html output but it only comes out correct on my local dev and not my staging server. 我有一些辅助方法来构建HTML输出,但只能在本地开发人员而不是在登台服务器上正确显示。 The ~ doesn't work since this is not Razor. 〜不起作用,因为这不是Razor。

My local site path is http://localhost/testSite 我的本地站点路径是http:// localhost / testSite
My staging path is http://192.168.1.2/ 我的登台路径是http://192.168.1.2/

On my local this produces src="/testSite/images.test.jpg" 在我的本地计算机上,这会产生src =“ / testSite / images.test.jpg”
On my staging instance it produces src="//images.test.jpg" 在我的暂存实例上,它生成src =“ // images.test.jpg”

var baseUrl = HttpRuntime.AppDomainAppPath;
var tb = new TagBuilder("img");
tb.MergeAttribute("title", "Main Image");
tb.MergeAttribute("src", baseUrl +"\images\test.jpg");
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));

I'm sure there is a method built in to do this on the code side 我确定在代码方面有内置的方法可以做到这一点

var baseUrl = System.Web.Mvc.UrlHelper.GenerateContentUrl("~/images/", HttpContext.Current); var baseUrl = System.Web.Mvc.UrlHelper.GenerateContentUrl(“〜/ images /”,HttpContext.Current);

Using the GenerateContentUrl you can keep using ~/ 使用GenerateContentUrl,您可以继续使用〜/

Ugh... after reviewing all the helpers and context objects available, I must conclude there is no function or method that provides exactly what you want (other than to use the tilde in one of the functions, as shown in the other answers). 嗯...在检查了所有可用的帮助器和上下文对象之后,我必须得出结论,没有任何函数或方法可以完全提供所需的内容(除了在其中一个函数中使用代字号,如其他答案所示)。 All of them require some sort of string processing (eg Split() or TrimEnd() ) in order to work. 它们都需要某种类型的字符串处理(例如Split()TrimEnd() )才能起作用。

Instead, Microsoft kindly gave us a band-aid function, AppendTrailingSlash , which you can use to solve the problem like this: 相反,Microsoft好心地给我们提供了一个创可贴函数AppendTrailingSlash ,您可以使用它解决以下问题:

var baseUrl = VirtualPathUtility.AppendTrailingSlash(HttpRuntime.AppDomainAppPath);
var tb = new TagBuilder("img");
tb.MergeAttribute("title", "Main Image");
tb.MergeAttribute("src", baseUrl +"images/test.jpg");  //notice I removed the initial slash
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));

AppendTrailingSlash is guaranteed not to "double up" on the forward slash when the virtual directory is "/", so this should work for both your staging and development environments. 当虚拟目录为“ /”时,保证AppendTrailingSlash不会在正斜杠上“加倍”,因此这对登台环境和开发环境均适用。

PS I'm pretty sure you should be using AppDomainAppVirtualPath and not AppDomainAppPath , which provides the physical path and not the virtual one. PS我很确定您应该使用AppDomainAppVirtualPath而不是AppDomainAppPath ,后者提供了物理路径而不是虚拟路径。

Hi you can try also with Using double dot notation in URL link 嗨,您也可以尝试在URL链接中使用双点符号

ex: 例如:

../file would mean to look in the parent directory for file. ../file表示要在父目录中查找文件。 If you had just file it would look in the current directory. 如果您只有文件,它将在当前目录中查找。

Thanks 谢谢

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

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