Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 288
Joe Audette Avatar asked Jul 13 '15 15:07

Joe Audette


People also ask

What is use of Wwwroot folder in ASP.NET Core?

By default, the wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root.

Where project Static files are stored in ASP.NET Core?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method.

Can we enable directory browsing through the code in ASP.NET Core?

You can enable directory browsing in an ASP.NET Core application by using the UseDirectoryBrowser extension method in the Configure method of the Startup class.

What is the configuration file for ASP NET MVC core app?

ASP.NET MVC configuration In ASP.NET apps, configuration uses the built-in . NET configuration files, web. config in the app folder and machine. config on the server.


1 Answers

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>
like image 184
craftworkgames Avatar answered Sep 30 '22 04:09

craftworkgames