blob: d3f78fd9a7cb15ee406f1cc9e406ef23f34a0ae3 (
plain)
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
|
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(
new CommonResponse(ErrorCodes.Common.UnknownEndpoint, Resource.MessageUnknownEndpoint),
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
context.Response.ContentLength = body.Length;
await context.Response.Body.WriteAsync(body);
await context.Response.CompleteAsync();
return;
}
await next();
});
}
}
}
|