【问题标题】:Reading a file in MVC 6在 MVC 6 中读取文件
【发布时间】:2015-06-04 23:49:02
【问题描述】:

我想访问我服务器主文件夹中的create.sql 文件。它包含设置我的数据库的查询。我根本无法访问此文件。

1) 我无法通过Configuration 真正到达那里。我只能使用AddJsonFileAddXmlFileAddIniFile。而且我想这不是将大 sql 文件放入其中任何一个的最佳主意。

2) Mvc source on github 似乎缺少 MapPath。所以不可能使用Server.MapPath("~/create.sql")

那么如何实现呢?

【问题讨论】:

  • MapPath 是 ASP.NET 的一部分。
  • 您可以使用 IApplicationEnvironment 服务上的 ApplicationBasePath 属性来获取应用程序的根路径...好奇,您要达到的场景是什么?
  • @KiranChalla 它有效,太好了,谢谢!如果您愿意,请将其写为答案,我会接受 :) 关于您的问题 - 我想在服务器启动时构建我的数据库结构(表、函数等)。

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

正如在 cmets 中已经注意到和提到的,在 ASP.NET VNext (MVC 6) 中似乎没有 MapPath。我在这里找到了解决方法:

http://forums.asp.net/t/2005166.aspx?HostingEnvironment+Equivalent+For+MapPath

基本上您需要从IApplicationEnvironment 接口获取ApplicationBasePath,该接口目前作为服务实现,如下解决方案:

    private readonly IApplicationEnvironment _appEnvironment;

    public HomeController(IApplicationEnvironment appEnvironment)
    {
        _appEnvironment = appEnvironment;
    }

    public IActionResult Index()
    {
        var rootPath = _appEnvironment.ApplicationBasePath;
        return View();
    }

【讨论】:

    【解决方案2】:

    另外,您可以使用PlatformServices.Default.Application.ApplicationBasePath,而不是注入IApplicationEnvironment

    编辑:这是 MapPath/UnmapPath 作为PlatformServices 的扩展的可能实现:

    removed (see EDIT2)
    

    EDIT2:稍作修改,添加了IsPathMapped() 以及一些检查是否真的需要路径映射/取消映射。

    public static class PlatformServicesExtensions
    {
        public static string MapPath(this PlatformServices services, string path)
        {
            var result = path ?? string.Empty;
            if (services.IsPathMapped(path) == false)
            {
                var wwwroot = services.WwwRoot();
                if (result.StartsWith("~", StringComparison.Ordinal))
                { 
                    result = result.Substring(1); 
                }
                if (result.StartsWith("/", StringComparison.Ordinal))
                { 
                    result = result.Substring(1);
                }
                result = Path.Combine(wwwroot, result.Replace('/', '\\'));
            }
    
            return result;
        }
    
        public static string UnmapPath(this PlatformServices services, string path)
        {
            var result = path ?? string.Empty;
            if (services.IsPathMapped(path))
            {
                var wwwroot = services.WwwRoot();
                result = result.Remove(0, wwwroot.Length);
                result = result.Replace('\\', '/');
    
                var prefix = (result.StartsWith("/", StringComparison.Ordinal) ? "~" : "~/");
                result = prefix + result;
            }
    
            return result;
        }
    
        public static bool IsPathMapped(this PlatformServices services, string path)
        {
            var result = path ?? string.Empty;
            return result.StartsWith(services.Application.ApplicationBasePath,
                StringComparison.Ordinal);
        }
    
        public static string WwwRoot(this PlatformServices services)
        {
            // todo: take it from project.json!!!
            var result = Path.Combine(services.Application.ApplicationBasePath, "wwwroot");
            return result;
        }
    }
    

    EDIT3: PlatformServices.WwwRoot() 返回实际执行路径,在.net core 2.0 中,DEBUG 模式为 xxx\bin\Debug\netcoreapp2.0,这显然不是必需的。相反,将PlatformServices 替换为IHostingEnvironment 并使用environment.WebRootPath

    【讨论】:

    • 发布完整的 .net core 2.0 / 2.1 示例?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多