C#classlibrary中的Server.Mappath

Shy*_*yju 54 c# server.mappath

我如何在C#类库类中使用server.mappath方法,该类充当我的MyLabbsite的BusinessLayer

Aar*_*ell 117

通过调用它?

var path = System.Web.HttpContext.Current.Server.MapPath("default.aspx");
Run Code Online (Sandbox Code Playgroud)

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

  • 最糟糕的解决方案 如果我们需要使用HTTP上下文中的业务层类,该怎么办? (14认同)
  • 这是正确答案http://stackoverflow.com/questions/12294458/asp-net-mvc-4-use-server-mappath-in-business-layer (6认同)

Ela*_*amy 22

您可以使用以下代码获取基本路径,并使用该路径附加所需的路径.

string  path = System.AppDomain.CurrentDomain.BaseDirectory;
Run Code Online (Sandbox Code Playgroud)


Phi*_*ert 7

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

  HttpContext.Current.Server.MapPath(...)
Run Code Online (Sandbox Code Playgroud)


Mah*_*vcs 5

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

HostingEnvironment.MapPath("~/file")
Run Code Online (Sandbox Code Playgroud)

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