C# 如何在类库项目中使用 Server.MapPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16009891/
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-09 23:41:15  来源:igfitidea点击:

How can I use Server.MapPath inside class library project

c#asp.netserver.mappath

提问by dotnetnoob

I have a web application thet has a number of class library projects. Some example code below.

我有一个 Web 应用程序,它有许多类库项目。下面的一些示例代码。

public static class LenderBL
{
    static string LenderXml { get { return "MyPathHere"; } }

    public static LenderColl GetLenders()
    {
        var serializer = new XmlSerializer(typeof(LenderColl));

        using (XmlReader reader = XmlReader.Create(LenderXml))
        {
            return (LenderColl)serializer.Deserialize(reader);
        }
    }
}

I would normally use Server.MapPath to get the path for the property LenderXml, but when I use it in a class library is returns the path of the parent solution, not that of the class library project.

我通常会使用 Server.MapPath 来获取属性 LenderXml 的路径,但是当我在类库中使用它时,会返回父解决方案的路径,而不是类库项目的路径。

Is there a way of getting the path for the class libary project itself?

有没有办法获得类库项目本身的路径?

Thanks in advance.

提前致谢。

回答by Freelancer

    var Mappingpath = System.Web.HttpContext.Current.Server.MapPath("pagename.aspx");

Hope its helpful.

希望它有帮助。

回答by Davin Tryon

Server.MapPathwill always run in the context of the web root. So, in your case the web root is the parent web project. While your class libraries (assemblies) look like separate projects, at runtime, they are all hosted in the web project process. So, there are a few things to consider about resources that are in the class libraries.

Server.MapPath将始终在 Web 根的上下文中运行。因此,在您的情况下,Web 根是父 Web 项目。虽然您的类库(程序集)看起来像单独的项目,但在运行时,它们都托管在 Web 项目进程中。因此,对于类库中的资源,需要考虑一些事项。

First, consider if it makes sense to keep resources in the class library. Maybe, you should have the xmlfile in the web project and just reference it in the class library. This would be equivilant to how MVC projects keep their views in the web project. However, I assume you can't do this.

首先,考虑在类库中保留资源是否有意义。也许,您应该xml在 web 项目中拥有该文件,并在类库中引用它。这将等同于 MVC 项目如何在 Web 项目中保留其视图。但是,我假设您不能这样做。

Second, you could change the build properties of your xmlfile. If you change the file to contentand copy-alwaysin its properties. The file will copy to the bin directory. Then Server.MapPathshould work because the file will be accessible.

其次,您可以更改xml文件的构建属性。如果您将文件更改为contentcopy-always在其属性中。该文件将复制到 bin 目录。然后Server.MapPath应该可以工作,因为该文件可以访问。

Third, you could make the resource an embedded resourceand then reference it in code. This is a way to keep all the resources local to a built assembly.

第三,您可以将资源设为嵌入式资源,然后在代码中引用它。这是一种将所有资源保持在构建程序集本地的方法。

Hope this helps.

希望这可以帮助。

回答by Menelaos Vergis

As I can understand you need the current assembly location

据我所知,您需要当前的装配位置

static public string CurrentAssemblyDirectory()
{
    string codeBase = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(codeBase);
    string path = Uri.UnescapeDataString(uri.Path);
    return Path.GetDirectoryName(path);
}