This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
Startup.cs
90 lines (76 loc) · 2.67 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
87
88
89
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
namespace Builder.Middleware.Web
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseXHttpHeaderOverride();
app.UseMiddleware(typeof(MyMiddleware), "Yo");
}
public static void Main(string[] args)
{
var config = new ConfigurationBuilder().AddCommandLine(args).Build();
var host = new WebHostBuilder()
// We set the server by name before default args so that command line arguments can override it.
// This is used to allow deployers to choose the server for testing.
.UseKestrel()
.UseConfiguration(config)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
public static class BuilderExtensions
{
public static IApplicationBuilder UseXHttpHeaderOverride(this IApplicationBuilder builder)
{
return builder.UseMiddleware(typeof(XHttpHeaderOverrideMiddleware));
}
}
public class XHttpHeaderOverrideMiddleware
{
private readonly RequestDelegate _next;
public XHttpHeaderOverrideMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext)
{
var headerValue = httpContext.Request.Headers["X-HTTP-Method-Override"];
var queryValue = httpContext.Request.Query["X-HTTP-Method-Override"];
if (!string.IsNullOrEmpty(headerValue))
{
httpContext.Request.Method = headerValue;
}
else if (!string.IsNullOrEmpty(queryValue))
{
httpContext.Request.Method = queryValue;
}
return _next.Invoke(httpContext);
}
}
public class MyMiddleware
{
private RequestDelegate _next;
private string _greeting;
private IServiceProvider _services;
public MyMiddleware(RequestDelegate next, string greeting, IServiceProvider services)
{
_next = next;
_greeting = greeting;
_services = services;
}
public async Task Invoke(HttpContext httpContext)
{
await httpContext.Response.WriteAsync(_greeting + ", middleware!\r\n");
await httpContext.Response.WriteAsync("This request is a " + httpContext.Request.Method + "\r\n");
}
}
}