Skip to content

Commit 1dce073

Browse files
Arman Ossi Lokokhellang
authored andcommitted
Added a web example for SpaFallback
1 parent fbec959 commit 1dce073

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

Hellang.Middleware.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProblemDetails.MinimalApiSa
3838
EndProject
3939
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProblemDetails.Mvc.Sample", "samples\ProblemDetails.Mvc.Sample\ProblemDetails.Mvc.Sample.csproj", "{A4B33BF5-626A-4DEF-9EB0-71A49CB1B261}"
4040
EndProject
41+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpaFallback.Sample", "samples\SpaFallback.Sample\SpaFallback.Sample.csproj", "{3B90D096-9319-4ECF-B7B9-8BCBD0DFC239}"
42+
EndProject
4143
Global
4244
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4345
Debug|Any CPU = Debug|Any CPU
@@ -80,6 +82,10 @@ Global
8082
{A4B33BF5-626A-4DEF-9EB0-71A49CB1B261}.Debug|Any CPU.Build.0 = Debug|Any CPU
8183
{A4B33BF5-626A-4DEF-9EB0-71A49CB1B261}.Release|Any CPU.ActiveCfg = Release|Any CPU
8284
{A4B33BF5-626A-4DEF-9EB0-71A49CB1B261}.Release|Any CPU.Build.0 = Release|Any CPU
85+
{3B90D096-9319-4ECF-B7B9-8BCBD0DFC239}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
86+
{3B90D096-9319-4ECF-B7B9-8BCBD0DFC239}.Debug|Any CPU.Build.0 = Debug|Any CPU
87+
{3B90D096-9319-4ECF-B7B9-8BCBD0DFC239}.Release|Any CPU.ActiveCfg = Release|Any CPU
88+
{3B90D096-9319-4ECF-B7B9-8BCBD0DFC239}.Release|Any CPU.Build.0 = Release|Any CPU
8389
EndGlobalSection
8490
GlobalSection(SolutionProperties) = preSolution
8591
HideSolutionNode = FALSE
@@ -94,6 +100,7 @@ Global
94100
{1B9094D8-74F4-43BC-A119-E40573DE40FC} = {17F15D82-AC56-4579-BF5F-E53539AC0DB4}
95101
{6D090B43-0306-45A5-B4AB-CC2BA0AE22DC} = {A978F7AD-DFCC-41C4-9AEE-9E60D58A50FD}
96102
{A4B33BF5-626A-4DEF-9EB0-71A49CB1B261} = {A978F7AD-DFCC-41C4-9AEE-9E60D58A50FD}
103+
{3B90D096-9319-4ECF-B7B9-8BCBD0DFC239} = {A978F7AD-DFCC-41C4-9AEE-9E60D58A50FD}
97104
EndGlobalSection
98105
GlobalSection(ExtensibilityGlobals) = postSolution
99106
SolutionGuid = {1EB34854-E4E8-4AFD-91EB-C33FAD8F1725}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Hellang.Middleware.SpaFallback;
2+
using Microsoft.Extensions.FileProviders;
3+
4+
var folder = Path.Combine(AppContext.BaseDirectory, "StaticFiles");
5+
string? requestPath = null;
6+
var options = new StaticFileOptions
7+
{
8+
RequestPath = requestPath,
9+
FileProvider = new PhysicalFileProvider(folder)
10+
};
11+
12+
var builder = WebApplication.CreateBuilder(args);
13+
builder.Services.AddSpaFallback(y => y.AllowFileExtensions = true);
14+
15+
var app = builder.Build();
16+
app.UseSpaFallback().UseStaticFiles(options).Use(HandleRoutes);
17+
app.Run();
18+
19+
Task HandleRoutes(HttpContext context, Func<Task> next)
20+
{
21+
if (context.Request.Path.StartsWithSegments("/soft-404"))
22+
{
23+
context.Response.StatusCode = StatusCodes.Status404NotFound;
24+
return Task.CompletedTask;
25+
}
26+
27+
if (context.Request.Path.StartsWithSegments("/no-content"))
28+
{
29+
context.Response.StatusCode = StatusCodes.Status204NoContent;
30+
return Task.CompletedTask;
31+
}
32+
33+
return next();
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\SpaFallback\SpaFallback.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="StaticFiles\**\*.*">
15+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16+
</None>
17+
</ItemGroup>
18+
19+
</Project>
60.1 KB
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Test Page</title>
4+
</head>
5+
<body>
6+
<h1>This is the index file.</h1>
7+
</body>
8+
</html>

0 commit comments

Comments
 (0)