Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebrind/a6a2a6bdea9ae248da53ba71785edf1c to your computer and use it in GitHub Desktop.
Save mikebrind/a6a2a6bdea9ae248da53ba71785edf1c to your computer and use it in GitHub Desktop.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.Configure<RazorViewEngineOptions>(opts =>
opts.FileProviders.Add(
new DatabaseFileProvider(Configuration.GetConnectionString("DefaultConnection"))
)
);
}
@gyankov
Copy link

gyankov commented Jun 29, 2021

Hi Mike,

I saw your loading views from database article and it helped me a lot. However, in my scenario, code snippets are submitted from the client. Thus, I need a reference to the HttpContext in the FileProvider to get the view's content.
e.g.

    public class MyFileProvider : IFileProvider
    {
        private IHttpContextAccessor httpContextAccessor;

        public MyFileProvider (IHttpContextAccessor httpContextAccessor)
        {
            this.httpContextAccessor = httpContextAccessor;
        }
...

The problem is that I have to manually create an instance of the file provider in the Startup.cs, but I depend on DI to inject httpContextAccessor and creating the instance myself is not an option.

Do you have any ideas how to inject dependencies which will change per request to the file provider?

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment