in asp.net 5 is it possible to store and read custom files in approot instead of wwwroot?

拟墨画扇 提交于 2019-12-09 09:25:31

问题


when you deploy an asp.net5/mvc6 app there is the wwwroot folder where web assets like css, js, images belong, and there is approot folder where packages and source code belong. It seems that classes in the Microsoft.Framework.Configuration namespace for example must be able to read files from below approot since that is where config.json files would live.

What I want to know is, is it possible to store and read custom files of my own in approot? and if so how?

For example I'm not using Entity Framework so I need a place to put sql install and upgrade scripts and would prefer not to put them beneath wwwroot. I also have custom configuration files for things like navigation sitemap that I would rather not put below wwwroot if it is possible to put them elsewhere such as approot.

I know I can access files below wwwroot using IHostingEnvironment env.MapPath("~/somefileinwwwrootfoilder.json")

Is there a similar way to access files under approot?


回答1:


Yes, it is possible. Just get the path to your app folder and the pass it to configuration or whoever else needs it:

public class Startup
{
    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        var wwwrootRoot = env.WebRootPath;
        var appRoot = appEnv.ApplicationBasePath;



回答2:


The accepted answer is correct, but since a ASP.NET Core 1.0 release a few things have changed so I thought I'd write a new clear things up a bit.

What I did was create a folder in my project called AppData. You can call it anything you like.

Note: it's not in wwwroot because we want to keep this data private.

Next, you can use IHostingEnvironment to get access to the folder path. This interface can be injected as a dependency into some kind of helper service and what you end up with is something like this:

public class AppDataHelper
{
    private readonly IHostingEnvironment _hostingEnvironment;
    private const string _appDataFolder = "AppData";

    public AppDataHelper(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public async Task<string> ReadAllTextAsync(string relativePath)
    {
        var path = Path.Combine(_hostingEnvironment.ContentRootPath, _appDataFolder, relativePath);

        using (var reader = File.OpenText(path))
        {
            return await reader.ReadToEndAsync();
        }
    }
}

Additionally, to get things to deploy correctly I had to add the AppData folder to the publishOptions include list in project.json.

As mentioned in the comments, to deploy AppData folder correctly in ASP.NET MVC Core 2 (using *.csproj file, instead of project.json), syntax is as follows:

  <ItemGroup>
    <None Include="AppData\*" CopyToPublishDirectory="PreserveNewest" />
  </ItemGroup>


来源:https://stackoverflow.com/questions/31387483/in-asp-net-5-is-it-possible-to-store-and-read-custom-files-in-approot-instead-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!