Server.Mappath in C# classlibrary

2019-01-22 05:14发布

How can i use the server.mappath method in a C# class library class ,which acts as my BusinessLayer for My ASP.NET WEbsite

7条回答
等我变得足够好
2楼-- · 2019-01-22 05:49

By calling it?

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

Make sure you add a reference to the System.Web assembly.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-22 05:49

You should reference System.Web and call:

  HttpContext.Current.Server.MapPath(...)
查看更多
再贱就再见
4楼-- · 2019-01-22 06:01

Maybe you could abstract this as a dependency and create an IVirtualPathResolver. This way your service classes wouldn't be bound to System.Web and you could create another implementation if you wanted to reuse your logic in a different UI technology.

查看更多
Luminary・发光体
5楼-- · 2019-01-22 06:02

Use this System.Web.Hosting.HostingEnvironment.MapPath().

HostingEnvironment.MapPath("~/file")

Wonder why nobody mentioned it here.

查看更多
6楼-- · 2019-01-22 06:05

Architecturally, System.web should not be referred in Business Logic Layer (BLL). Employ BLL into the solution structure to follow the separate of concern principle so refer System.Web is a bad practice. BLL should not load/run in Asp.net context. Because of the reason you should consider using of System.AppDomain.CurrentDomain.BaseDirectory instead of System.Web.HttpContext.Current.Server.MapPath

查看更多
The star\"
7楼-- · 2019-01-22 06:09
HostingEnvironment.MapPath
System.Web.Hosting.HostingEnvironment.MapPath(path);
查看更多
登录 后发表回答