-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStartup.cs
86 lines (73 loc) · 2.59 KB
/
Startup.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System.IO;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using JNCC.Microsite.SAC.Helpers;
namespace JNCC.Microsite.SAC
{
public class Startup
{
public Startup(IHostingEnvironment env, ILogger<Startup> log)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.WebRootPath)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Webserver Root is at <root>/output/html
Console.WriteLine("Webserver root is {0}", env.WebRootPath);
// Static files Root is at <root>/docs
string staticFilesRoot = FileHelper.GetActualFilePath(env.WebRootPath, Path.Combine("..", "..", "docs"));
Console.WriteLine("Static files root is {0}", staticFilesRoot);
app.UseDefaultFiles()
.UseStaticFiles()
.UseRequestInterceptorMiddleware();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(staticFilesRoot)
});
}
}
public class RequestInterceptorMiddleware
{
private readonly RequestDelegate _next;
public RequestInterceptorMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
await _next(context);
if (context.Response.StatusCode == 404)
{
if (context.Request.Path.ToString().EndsWith(".html"))
{
context.Response.Redirect("/404.html", false);
}
else
{
context.Response.Redirect(context.Request.Path + ".html", false);
}
}
}
}
public static class RequestInterceptorMiddlewareExtensions
{
public static IApplicationBuilder UseRequestInterceptorMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RequestInterceptorMiddleware>();
}
}
}