C#类库中的Server.Mappath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1199486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:43:26  来源:igfitidea点击:

Server.Mappath in C# classlibrary

c#server.mappath

提问by Shyju

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

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

采纳答案by Aaron Powell

By calling it?

通过调用它?

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

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

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

回答by Philippe Leybaert

You should reference System.Web and call:

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

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

回答by Tom Miller

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.

也许您可以将其抽象为依赖项并创建一个 IVirtualPathResolver。这样您的服务类就不会绑定到 System.Web,如果您想在不同的 UI 技术中重用您的逻辑,您可以创建另一个实现。

回答by Elanchezhian Narayanasamy

You can get the base path by using the following code and append your needed path with that.

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

string  path = System.AppDomain.CurrentDomain.BaseDirectory;

回答by Mahmoodvcs

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

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

HostingEnvironment.MapPath("~/file")

Wonder why nobody mentioned it here.

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

回答by Sachin Panchal

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

回答by CodeSi

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.BaseDirectoryinstead of System.Web.HttpContext.Current.Server.MapPath

在体系结构上,不应在业务逻辑层 (BLL) 中引用 System.web。在解决方案结构中使用 BLL 以遵循关注点分离原则,因此参考 System.Web 是一种不好的做法。BLL 不应在 Asp.net 上下文中加载/运行。由于您应该考虑使用 ofSystem.AppDomain.CurrentDomain.BaseDirectory而不是System.Web.HttpContext.Current.Server.MapPath