简体   繁体   English

我怎样才能使用global.asax中的Server.MapPath()?

[英]How can I use Server.MapPath() from global.asax?

I need to use Server.MapPath() to combine some files path that I store in the web.config . 我需要使用Server.MapPath()来组合我存储在web.config中的一些文件路径。

However, since Server.MapPath() relies on the current HttpContext (I think), I am unable to do this. 但是,由于Server.MapPath()依赖于当前的HttpContext(我认为),我无法做到这一点。 When trying to use the method, even though its "available", I get the following exception: 在尝试使用该方法时,即使它“可用”,我也会遇到以下异常:

Server operation is not available in this context. 在此上下文中无法使用服务器操作。

Is there another method that can map a web root relative directory such as ~/App_Data/ to the full physical path such as C:\\inetpub\\wwwroot\\project\\App_data\\ ? 是否有另一种方法可以将Web根目录相对目录(如~/App_Data/映射到完整的物理路径,例如C:\\inetpub\\wwwroot\\project\\App_data\\

You could try System.Web.Hosting.HostingEnvironment.MapPath() . 您可以尝试System.Web.Hosting.HostingEnvironment.MapPath()

No HttpContext required. 不需要HttpContext。

使用AppDomain.CurrentDomain.BaseDirectory因为Context可能会返回null!

When in Global.asax, use the context object: 在Global.asax中时,使用上下文对象:

context.Server.mappath() context.Server.mappath()

Context lets you access also the session collection, the request object, the response object. Context还允许您访问会话集合,请求对象和响应对象。 Very useful when you want to log errors, for example 例如,当您想记录错误时非常有用

您可以尝试HttpContext.Current.Server.MapPath(“/”) - 这就是我之前在类中引用它的方式。

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

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