在MVC 6中读取文件

Pat*_*ski 8 c# asp.net-core-mvc asp.net-core

我想create.sql在我的服务器的主文件夹中访问我的文件.它包含用于设置数据库的查询.我有一个问题,根本无法访问此文件.

1)我无法真正实现目标Configuration.我只能用AddJsonFile,AddXmlFileAddIniFile.我想这不是一个把大sql文件放到其中任何一个的最好的主意.

2)github上的Mvc源似乎丢失了MapPath.所以不可能使用Server.MapPath("~/create.sql").

那么如何实现呢?

J. *_*non 16

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

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

基本上,您需要在解决方案下方获取当前作为服务实现的ApplicationBasePathfrom IApplicationEnvironment接口:

    private readonly IApplicationEnvironment _appEnvironment;

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

    public IActionResult Index()
    {
        var rootPath = _appEnvironment.ApplicationBasePath;
        return View();
    }
Run Code Online (Sandbox Code Playgroud)


Ale*_*tov 5

而且,IApplicationEnvironment您可以使用而不是注射PlatformServices.Default.Application.ApplicationBasePath.

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

removed (see EDIT2)
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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