Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC6中读取文件_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 在MVC6中读取文件

C# 在MVC6中读取文件,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我想访问服务器主文件夹中的create.sql文件。它包含用于设置数据库的查询。我访问此文件时遇到问题 1) 通过配置我真的无法到达那里。我只能使用AddJsonFile、AddXmlFile和AddIniFile。我想这并不是将一个大的sql文件放入其中的最好方法 2) 似乎缺少MapPath。因此不可能使用Server.MapPath(“~/create.sql”) 那么如何实现这一点呢?正如在评论中已经注意到和提到的那样,ASP.NET VNext(MVC 6)中似乎没有MapPath。我

我想访问服务器主文件夹中的
create.sql
文件。它包含用于设置数据库的查询。我访问此文件时遇到问题

1) 通过
配置
我真的无法到达那里。我只能使用
AddJsonFile
AddXmlFile
AddIniFile
。我想这并不是将一个大的sql文件放入其中的最好方法

2) 似乎缺少
MapPath
。因此不可能使用
Server.MapPath(“~/create.sql”)


那么如何实现这一点呢?

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

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

    private readonly IApplicationEnvironment _appEnvironment;

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

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

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

编辑:这里有一个可能的MapPath/unappath实现,作为
平台服务的扩展

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的调试模式下,它是xxx\bin\DEBUG\netcoreapp2.0,这显然不是必需的。相反,将
PlatformServices
替换为
IHostingEnvironment
并使用
environment。WebRootPath

MapPath
是ASP.NET的一部分。您可以使用IAApplicationEnvironment服务上的ApplicationBasePath属性来获取应用程序的根路径…奇怪,你想实现什么样的场景?@KiranChalla它很有效,太好了,谢谢你!如果您愿意,请将其作为回答,我将接受:)关于您的问题-我想在服务器启动时构建数据库的结构(表、函数等)。是否发布完整的.net core 2.0/2.1示例?