aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Routes/UnknownEndpointMiddleware.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-06-14 16:34:42 +0800
committercrupest <crupest@outlook.com>2020-06-14 16:34:42 +0800
commit5e02abc95cc8419b378917d7053d63092046b2cf (patch)
treeace855832f1c4e88e9fed7b2b2c95fb5e06424f3 /Timeline/Routes/UnknownEndpointMiddleware.cs
parent4c6f4c214edc5b574951d4cc8f3654bc30efa8da (diff)
downloadtimeline-5e02abc95cc8419b378917d7053d63092046b2cf.tar.gz
timeline-5e02abc95cc8419b378917d7053d63092046b2cf.tar.bz2
timeline-5e02abc95cc8419b378917d7053d63092046b2cf.zip
Many many bugs fix.
1. Add a way to test front end with mock page. 2. Unknown api returns 400 but not frontend page. 3. Fix a big bug that cause all data loss in database migration.
Diffstat (limited to 'Timeline/Routes/UnknownEndpointMiddleware.cs')
-rw-r--r--Timeline/Routes/UnknownEndpointMiddleware.cs39
1 files changed, 39 insertions, 0 deletions
diff --git a/Timeline/Routes/UnknownEndpointMiddleware.cs b/Timeline/Routes/UnknownEndpointMiddleware.cs
new file mode 100644
index 00000000..25ec563c
--- /dev/null
+++ b/Timeline/Routes/UnknownEndpointMiddleware.cs
@@ -0,0 +1,39 @@
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Http;
+using System;
+using System.Net.Mime;
+using System.Text.Json;
+using Timeline.Models.Http;
+
+namespace Timeline.Routes
+{
+ public static class UnknownEndpointMiddleware
+ {
+ public static void Attach(IApplicationBuilder app)
+ {
+ app.Use(async (context, next) =>
+ {
+ if (context.GetEndpoint() != null)
+ {
+ await next();
+ return;
+ }
+
+ if (context.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase))
+ {
+ context.Response.StatusCode = StatusCodes.Status400BadRequest;
+ context.Response.ContentType = MediaTypeNames.Application.Json;
+
+ var body = JsonSerializer.SerializeToUtf8Bytes(ErrorResponse.Common.UnknownEndpoint(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
+
+ context.Response.ContentLength = body.Length;
+ await context.Response.Body.WriteAsync(body);
+ await context.Response.CompleteAsync();
+ return;
+ }
+
+ await next();
+ });
+ }
+ }
+}