Server.Mappath在C#中的classlibrary

我如何使用C#类库类中的server.mappath方法,它充当我的BusinessLayer我的ASP.NET Web站点

通过调用它?

var path = System.Web.HttpContext.Current.Server.MapPath("default.aspx"); 

确保您添加对System.Web程序集的引用。

你可以通过使用下面的代码获得基本path,并附加你所需要的path。

 string path = System.AppDomain.CurrentDomain.BaseDirectory; 

您应该引用System.Web并调用:

  HttpContext.Current.Server.MapPath(...) 

也许你可以把它作为一个依赖关系来创build一个IVirtualPathResolver。 这样,你的服务类就不会绑定到System.Web,如果你想在不同的UI技术中重用你的逻辑,你可以创build另一个实现。

HostingEnvironment.MapPath

System.Web.Hosting.HostingEnvironment.MapPath(path);

使用这个System.Web.Hosting.HostingEnvironment.MapPath() 。

 HostingEnvironment.MapPath("~/file") 

想知道为什么没有人在这里提到

在结构上,System.web不应该在业务逻辑层(BLL)中被引用。 将BLL应用到解决scheme结构中以遵循关注原则的分离,因此请参阅System.Web是一个不好的做法。 BLL不应该在Asp.net上下文中加载/运行。 因为你应该考虑使用System.AppDomain.CurrentDomain.BaseDirectory而不是System.Web.HttpContext.Current.Server.MapPath