I am trying to get the instance of HttpContext
from the IFileProvider
, so I can access the original path requested before the route mapping updated in the subpath.
I am trying out a sample to read view dynamically from a database, but as the path is always mapped to the main controller, and the views cached I can't get access to the original request path to load the correct view from the database. The only way I could make it work is to hardcore the mappings and have respectively different controller or actions handling each file, but then it won't be using the dynamic views from the database.
public class DBViewProvider : IFileProvider {
public IDirectoryContents GetDirectoryContents(string subpath) {
string path = ConvertPath(subpath);
return new DBViewDirectoryContents(path);
}
The dependency injection will help you inject the HttpContextAccessor
in the constructor
public class DBViewProvider : IFileProvider {
private readonly IHttpContextAccessor httpContextAccessor;
public DBViewProvider(IHttpContextAccessor httpContextAccessor){
this.httpContextAccessor = httpContextAccessor;
}
public IDirectoryContents GetDirectoryContents(string subpath) {
HttpContext httpContext = httpContextAccessor.HttpContext;
string path = ConvertPath(subpath);
return new DBViewDirectoryContents(path);
}
/*******************hidden for brievety************/
}
You will need to add this line in the Startup.ConfigureServices
method like this
services.AddHttpContextAccessor();