-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseWrite.cs
More file actions
46 lines (39 loc) · 1.27 KB
/
Copy pathResponseWrite.cs
File metadata and controls
46 lines (39 loc) · 1.27 KB
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
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
public static class ResponseWriteExtensions
{
public static void Write(this HttpResponse response, string text)
{
if (string.IsNullOrEmpty(text))
return;
if (response.HttpContext.Items.ContainsKey("Elanat.ResponseWrite"))
response.HttpContext.Items["Elanat.ResponseWrite"] += text;
else
response.HttpContext.Items["Elanat.ResponseWrite"] = text;
}
}
public class ResponseWriteMiddleware
{
private readonly RequestDelegate _next;
public ResponseWriteMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
await _next(context);
if (context.Items.ContainsKey("Elanat.ResponseWrite"))
{
var text = context.Items["Elanat.ResponseWrite"]?.ToString();
if (!string.IsNullOrEmpty(text))
await context.Response.WriteAsync(text);
}
}
}
public static class ResponseWriteMiddlewareExtensions
{
public static IApplicationBuilder ResponseWrite(this IApplicationBuilder builder)
{
return builder.UseMiddleware<ResponseWriteMiddleware>();
}
}