开发者

How to load a definition file at application start located in a virtual path

开发者 https://www.devze.com 2023-01-04 18:57 出处:网络
How do I load a definition file which contains some start up logic at Application_Start? I only know the virtual path but not the server physical path. How do I convert the开发者_开发技巧 virtual pat

How do I load a definition file which contains some start up logic at Application_Start?

I only know the virtual path but not the server physical path. How do I convert the开发者_开发技巧 virtual path to server path without Server.MapPath()

I am not sure I can access httpcontext or not in the application start stage.

Thanks


I think there is a little misconception around the Application_Start method and the Global class itself. A new instance of the Global class is created for every request, but the Application_Start method is only fired the first time the application runs.

You can actually access the HttpContext.Current.Server property from here just fine:

var server = HttpContext.Current.Server;
string path = server.MapPath(...);


"I am not sure I can access httpcontext or not in the application start stage."

You cannot use the Request object or anything from the HttpContext object in Application_Start when you are using IIS7 or higher (it does work with IIS6 though). However, getting the physical application path can simply be done with Server.MapPath as follows:

string physicalPath = Server.MapPath("~");

Not sure why you do not wish to use Server.MapPath, but it is a sure way of getting the location in a safe manner and it works well in Application_Start.

As you know now, you cannot get the current context in Application_Start as I said earlier, because there is not necessarily one. You can still use Server.MapPath as above, or, if you mustn't use Server.MapPath for some reason, you can use HttpRuntime.AppDomainPath which works everywhere.

string physicalPath = HttpRuntime.AppDomainPath;
0

精彩评论

暂无评论...
验证码 换一张
取 消