Skip to content

Commit

Permalink
Add MapStaticAssets.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Sep 23, 2024
1 parent a4e6519 commit 361902f
Show file tree
Hide file tree
Showing 39 changed files with 82 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using System;
using System.IO;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.RequestLocalization;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.StaticAssets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Volo.Abp;
using Volo.Abp.AspNetCore.Auditing;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.Security;
using Volo.Abp.AspNetCore.Security.Claims;
using Volo.Abp.AspNetCore.Tracing;
using Volo.Abp.AspNetCore.Uow;
using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
using Volo.Abp.VirtualFileSystem;

namespace Microsoft.AspNetCore.Builder;

Expand Down Expand Up @@ -117,4 +124,28 @@ public static IApplicationBuilder UseDynamicClaims(this IApplicationBuilder app)
{
return app.UseMiddleware<AbpDynamicClaimsMiddleware>();
}

public static StaticAssetsEndpointConventionBuilder MapStaticAssets(this IApplicationBuilder app, string? staticAssetsManifestPath = null)
{
if (app is not IEndpointRouteBuilder endpoints)
{
throw new AbpException("The app(IApplicationBuilder) is not an IEndpointRouteBuilder.");
}

var contentTypeProvider = endpoints.ServiceProvider.GetRequiredService<AbpFileExtensionContentTypeProvider>();
var webContentFileProvider = new WebContentFileProvider(
endpoints.ServiceProvider.GetRequiredService<IVirtualFileProvider>(),
null,
endpoints.ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreContentOptions>>()
);

app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider = contentTypeProvider,
FileProvider = webContentFileProvider
});

var result = endpoints.MapStaticAssets(staticAssetsManifestPath);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public class WebContentFileProvider : IWebContentFileProvider, ISingletonDepende
{
private readonly IVirtualFileProvider _virtualFileProvider;
private readonly IFileProvider _fileProvider;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IWebHostEnvironment? _hostingEnvironment;
private string _rootPath = "/wwwroot";

protected AbpAspNetCoreContentOptions Options { get; }

public WebContentFileProvider(
IVirtualFileProvider virtualFileProvider,
IWebHostEnvironment hostingEnvironment,
IWebHostEnvironment? hostingEnvironment,
IOptions<AbpAspNetCoreContentOptions> options)
{
_virtualFileProvider = virtualFileProvider;
Expand Down Expand Up @@ -85,23 +85,25 @@ public virtual IChangeToken Watch(string filter)
return new CompositeChangeToken(
new[]
{
_fileProvider.Watch(_rootPath + filter),
_fileProvider.Watch(filter)
_fileProvider.Watch(_rootPath + filter),
_fileProvider.Watch(filter)
}
);
}

protected virtual IFileProvider CreateFileProvider()
{
var fileProviders = new List<IFileProvider>
{
new PhysicalFileProvider(_hostingEnvironment.ContentRootPath),
_virtualFileProvider
};
{
_virtualFileProvider
};

return new CompositeFileProvider(
fileProviders
);
if (_hostingEnvironment != null)
{
fileProviders.Add(_hostingEnvironment.WebRootFileProvider);
}

return new CompositeFileProvider(fileProviders);
}

protected virtual bool ExtraAllowedFolder(string path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseRouting();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseConfiguredEndpoints();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseConfiguredEndpoints();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
app.UseErrorPage();
}

app.UseStaticFiles();
app.MapStaticAssets();

app.UseRouting();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseConfiguredEndpoints();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseHttpsRedirection();
app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors(DefaultCorsPolicyName);
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseHttpsRedirection();
app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors(DefaultCorsPolicyName);
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();

Expand Down
2 changes: 1 addition & 1 deletion modules/docs/app/VoloDocs.Web/VoloDocsWebModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();

app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
Expand Down
2 changes: 1 addition & 1 deletion modules/openiddict/app/OpenIddict.Demo.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
{
var app = context.GetApplicationBuilder();

app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAbpRequestLocalization();
app.UseConfiguredEndpoints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseHttpsRedirection();
app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAbpOpenIddictValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseHttpsRedirection();
app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAbpOpenIddictValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseCorrelationId();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseCorrelationId();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAbpOpenIddictValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAbpOpenIddictValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseHttpsRedirection();
app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAbpOpenIddictValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseHttpsRedirection();
app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAbpOpenIddictValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

app.UseHttpsRedirection();

app.UseStaticFiles();
app.MapStaticAssets();
app.UseAntiforgery();

app.MapRazorComponents<App>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseAbpRequestLocalization();
app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Extensions.DependencyInjection;
Expand Down Expand Up @@ -225,7 +226,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseAuthentication();
app.UseAbpOpenIddictValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public async override Task OnApplicationInitializationAsync(ApplicationInitializ

app.UseHttpsRedirection();
app.UseCorrelationId();
app.UseStaticFiles();
app.MapStaticAssets();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
Expand Down
Loading

0 comments on commit 361902f

Please sign in to comment.