How to access the HttpServerUtility.MapPath method in a Thread or Timer?

前端 未结 6 1908
梦毁少年i
梦毁少年i 2020-12-23 15:46

I use a System.Timers.Timer in my Asp.Net application and I need to use the HttpServerUtility.MapPath method which seems to be only available via <

6条回答
  •  一向
    一向 (楼主)
    2020-12-23 16:16

    It's possible to use HostingEnvironment.MapPath() instead of HttpContext.Current.Server.MapPath()

    I haven't tried it yet in a thread or timer event though.


    Some (non viable) solutions I considered;

    • The only method I care about on HttpServerUtility is MapPath. So as an alternative I could use AppDomain.CurrentDomain.BaseDirectory and build my paths from this. But this will fail if your app uses virtual directories (Mine does).

    • Another approach: Add all the paths I need to the the Global class. Resolve these paths in Application_Start.

提交回复
热议问题