forked from ExtCore/ExtCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddStaticFilesAction.cs
More file actions
52 lines (47 loc) · 2.03 KB
/
AddStaticFilesAction.cs
File metadata and controls
52 lines (47 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright © 2017 Dmitry Sikorsky. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using ExtCore.Infrastructure;
using ExtCore.Infrastructure.Actions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
namespace ExtCore.Mvc.Actions
{
/// <summary>
/// Implements the <see cref="IConfigureServicesAction">IConfigureServicesAction</see> interface and
/// creates and registers the composite file provider that contains resources from all the extensions.
/// </summary>
public class AddStaticFilesAction : IConfigureServicesAction
{
/// <summary>
/// Priority of the action. The actions will be executed in the order specified by the priority (from smallest to largest).
/// </summary>
public int Priority => 1000;
/// <summary>
/// Creates and registers the composite file provider that contains resources from all the extensions.
/// </summary>
/// <param name="services">
/// Will be provided by the ExtCore and might be used to register any service inside the DI.
/// </param>
/// <param name="serviceProvider">
/// Will be provided by the ExtCore and might be used to get any service that is registered inside the DI at this moment.
/// </param>
public void Execute(IServiceCollection services, IServiceProvider serviceProvider)
{
serviceProvider.GetService<IWebHostEnvironment>().WebRootFileProvider = this.CreateCompositeFileProvider(serviceProvider);
}
private IFileProvider CreateCompositeFileProvider(IServiceProvider serviceProvider)
{
IFileProvider[] fileProviders = new IFileProvider[] {
serviceProvider.GetService<IWebHostEnvironment>().WebRootFileProvider
};
return new CompositeFileProvider(
fileProviders.Concat(
ExtensionManager.Assemblies.Select(a => new EmbeddedFileProvider(a, a.GetName().Name))
)
);
}
}
}