在MVC 6中读取文件

8
我想要访问服务器主文件夹中的create.sql文件,其中包含设置我的数据库的查询。但我无法访问此文件。
1)我不能通过Configuration真正到达那里。我只能使用AddJsonFileAddXmlFileAddIniFile。我猜在这些中放置一个大型sql文件不是最好的选择。
2)Github上的Mvc源代码似乎缺少MapPath。因此无法使用Server.MapPath("~/create.sql")
那么如何实现呢?

MapPath 是 ASP.NET 的一部分。 - Daniel A. White
3
您可以使用IApplicationEnvironment服务上的ApplicationBasePath属性来获取您的应用程序的根路径...好奇,您试图实现什么场景? - Kiran
@KiranChalla,太棒了,它运行良好,谢谢!如果您愿意,请将其编写为答案,我会接受它 :) 关于您的问题 - 我想在服务器启动时构建我的数据库结构(表、函数等)。 - Patryk Golebiowski
2个回答

16
正如在评论中已经注意到和提到的那样,似乎在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();
    }

5

此外,你可以使用PlatformServices.Default.Application.ApplicationBasePath代替注入IApplicationEnvironment

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

removed (see EDIT2)

编辑2: 稍作修改,添加了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 示例? - Joe Healy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接