Get application path without using httpcontext. (asp.net)

后端 未结 3 2019
死守一世寂寞
死守一世寂寞 2020-12-08 08:59

How to do it?

I don\'t want to use this:

HttpContext.Current.Server.MapPath

Is there a similar function that I can call without req

3条回答
  •  醉梦人生
    2020-12-08 09:39

    There are several options:

    HttpRuntime.AppDomainAppPath

        WebApplication     -> Web root folder
        UnitTest           -> ArgumentNullException
        ConsoleApplication -> ArgumentNullException
    

    AppDomain.CurrentDomain.BaseDirectory

        WebApplication     -> Web root folder
        UnitTest           -> ...\AppDir\bin\Debug
        ConsoleApplication -> ...\AppDir\bin\Debug
    

    HostingEnvironment.ApplicationPhysicalPath

        WebApplication     -> Web root folder
        UnitTest           -> null
        ConsoleApplication -> null
    

    I would recommend to use AppDomain.CurrentDomain.BaseDirectory, because it can be used in any type of project and it can be set up.

    You can for example set UnitTest BaseDirectory to point your web root folder the AppDomain.CurrentDomain.BaseDirectory by command:

    AppDomain.CurrentDomain.SetData("APPBASE", "path to your web root");
    

提交回复
热议问题