【问题标题】:Nancy: is there a Server.MapPath("~/") equivalent?Nancy:是否有等效的 Server.MapPath("~/")?
【发布时间】:2013-01-26 04:37:59
【问题描述】:

我似乎无法在 Nancy 框架中找到 System.Web.HttpContext.Current.Server.MapPath() 的 Nancy 等效项。

我只想加载一个与应用程序服务相关的文本文件。

我在程序集中看到了这个

using Nancy;
using System;

namespace Nancy.Hosting.Self
{
    public class FileSystemRootPathProvider : IRootPathProvider, IHideObjectMembers
    {
        public FileSystemRootPathProvider();

        public string GetRootPath();
    }
}

我不知道如何使用。

更新:我刚刚发现我需要加载的任何内容都可以从 bin/relase/ 目录中读取/写入。这是在 Nancy Self Hosting 环境中假设的方式吗?我想这是有道理的。

【问题讨论】:

    标签: c# path hosting nancy


    【解决方案1】:

    您可以依赖IRootPathProvider 并使用它来调用GetRootPath(),这将为您提供应用程序的根,您可以从那里添加(我建议使用Path.Combine

    【讨论】:

    • 我总是使用 path.combine。只需要一个生产质量项目就让我再也不会手动组合路径字符串 =)
    • 应该实现 IHideObjectMembers 吗?
    • 认为这只是智能感知/视觉工作室,所以这取决于你。这只是隐藏了代码完成的基本功能。
    • 如果它在NancyModule 中,那么只需将IRootPathProvider 添加到您的构造函数中,您就可以开始使用它来调用GetRootPath()
    • @TheCodeJunkie 我们如何从引导程序访问 IRootPathProvider ?
    【解决方案2】:

    如果你需要在一个静态类(例如HtmlHelpers 扩展)中需要这个,其中IRootPathProvider 依赖项不能被注入,至少AFAIK,你可以使用AppDomain.CurrentDomain.BaseDirectory,这是DefaultRootPathProvider 在下面使用的.Net 4.x 的引擎盖:https://github.com/NancyFx/Nancy/blob/master/src/Nancy/DefaultRootPathProvider.cs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 2021-01-03
      • 2016-10-26
      • 2023-03-04
      • 2012-02-26
      • 2010-09-22
      • 2019-11-02
      相关资源
      最近更新 更多