Reading a file in MVC 6

前端 未结 2 1415
时光说笑
时光说笑 2020-12-19 04:30

I want to access my create.sql file in the main folder of my server. It contains queries to set up my database. I have a problem to access this file at all.

2条回答
  •  不思量自难忘°
    2020-12-19 05:28

    As already noticed and mentioned in the comments it seems that there is no MapPath in ASP.NET VNext (MVC 6). I found the workaround here:

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

    Basically you need to get the ApplicationBasePath from IApplicationEnvironment interface, which currently is implemented as a service, following below the solution:

        private readonly IApplicationEnvironment _appEnvironment;
    
        public HomeController(IApplicationEnvironment appEnvironment)
        {
            _appEnvironment = appEnvironment;
        }
    
        public IActionResult Index()
        {
            var rootPath = _appEnvironment.ApplicationBasePath;
            return View();
        }
    

提交回复
热议问题