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

后端 未结 3 2037
死守一世寂寞
死守一世寂寞 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:50

    I have run across this question when looking for way to compute an URL (permalinks in the Web application) to provide in some e-mail notifications.

    These were generated on another thread, so HttpContext was not available and I wanted to avoid putting URL related information in the queue table used to generate the e-mails.

    The code:

    public static String GetCurrentAppDomainBasePath(String prefix = "http://")
    {
       return String.Format("{0}{1}{2}", 
          prefix,
          System.Net.Dns.GetHostEntry("").HostName, 
          System.Web.HttpRuntime.AppDomainAppVirtualPath
       );
    }
    

    The function returns the full virtual path like: http://full-host-name/AppName. Of course, there are some limitations: hardcoded protocol (http, https etc.) and using hostname instead of domain name (fails if multiple domains are defined on a single machine).

提交回复
热议问题