Using Server.MapPath() inside a static field in ASP.NET MVC

前端 未结 2 1643
小鲜肉
小鲜肉 2020-12-13 02:59

I\'m building an ASP.NET MVC site where I\'m using Lucene.Net for search queries. I asked a question here about how to properly structure Lucene.Net usage in an ASP.NET MVC

相关标签:
2条回答
  • 2020-12-13 03:58

    Try HostingEnvironment.MapPath, which is static.

    See this SO question for confirmation that HostingEnvironment.MapPath returns the same value as Server.MapPath: What is the difference between Server.MapPath and HostingEnvironment.MapPath?

    0 讨论(0)
  • 2020-12-13 03:58

    I think you can try this for calling in from a class

     System.Web.HttpContext.Current.Server.MapPath("~/SignatureImages/");
    

    *----------------Sorry I oversight, for static function already answered the question by adrift*

    System.Web.Hosting.HostingEnvironment.MapPath("~/SignatureImages/");
    

    Update

    I got exception while using System.Web.Hosting.HostingEnvironment.MapPath("~/SignatureImages/");

    Ex details : System.ArgumentException: The relative virtual path 'SignatureImages' is not allowed here. at System.Web.VirtualPath.FailIfRelativePath()

    Solution (tested in static webmethod)

    System.Web.HttpContext.Current.Server.MapPath("~/SignatureImages/"); Worked

    0 讨论(0)
提交回复
热议问题