首页 文章

带有Autofac的ASP.NET 5 beta8自定义文件提供程序

提问于
浏览
0

我们有一个默认的ASP.NET Web应用程序,我们希望在其中使用自定义文件提供程序 .

public class CustomFileProvider : IFileProvider
{
    public IDirectoryContents GetDirectoryContents(string subpath)
    {
        ...
    }

    public IFileInfo GetFileInfo(string subpath)
    {
        ...
    }

    public IChangeToken Watch(string filter)
    {
        ...
    }
}

当我们使用以下代码配置提供程序时,它工作正常,框架调用自定义文件提供程序 .

public void ConfigureServices(IServiceCollection services)
{
    // Add MVC services to the services container.
    services.AddMvc();

    services.Configure<RazorViewEngineOptions>(options =>
    {
        // replace the fileprovider with the custom file provider
        options.FileProvider = new CustomFileProvider();
    });
}

当我们使用Autofac等自定义di容器扩展配置时,不再调用自定义文件提供程序 . 这曾经在beta5中运行良好 .

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    // Add MVC services to the services container.
    services.AddMvc();

    services.Configure<RazorViewEngineOptions>(options =>
    {
        // replace the fileprovider with the custom file provider
        options.FileProvider = new CustomFileProvider();
    });

    // Create the Autofac container builder.
    var builder = new ContainerBuilder();

    // Populate the services.
    builder.Populate(services);

    // Build the container.
    var container = builder.Build();

    // Resolve and return the service provider.
    return container.Resolve<IServiceProvider>();
}

有关如何使用自定义文件提供程序的更改?

1 回答

相关问题