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

蹲街弑〆低调 提交于 2019-12-18 09:59:26

问题


I need to use Server.MapPath() to combine some files path that I store in the web.config.

However, since Server.MapPath() relies on the current HttpContext (I think), I am unable to do this. 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\ ?


回答1:


You could try System.Web.Hosting.HostingEnvironment.MapPath().

No HttpContext required.




回答2:


Use AppDomain.CurrentDomain.BaseDirectory because Context might return null !!




回答3:


When in Global.asax, use the context object:

context.Server.mappath()

Context lets you access also the session collection, the request object, the response object. Very useful when you want to log errors, for example




回答4:


You could try HttpContext.Current.Server.MapPath("/") - That's how I have referenced it before in classes.



来源:https://stackoverflow.com/questions/935940/how-can-i-use-server-mappath-from-global-asax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!