简体   繁体   中英

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 .

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\\ ?

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

No HttpContext required.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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