Created
June 29, 2020 01:02
-
-
Save smaglio81/24a8d750197a0cd964806dd2c84cf614 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Http.Features; | |
namespace YourNamespace | |
{ | |
public static class HttpRequestExtensions | |
{ | |
public static bool CanReadBody(this HttpRequest request) | |
{ | |
return ( | |
request.Method == HttpMethods.Post | |
|| request.Method == HttpMethods.Put | |
) | |
&& request.Body.CanRead; | |
} | |
} | |
public class EnableBufferingMiddleware | |
{ | |
private readonly RequestDelegate _next; | |
public EnableBufferingMiddleware( | |
RequestDelegate next | |
) | |
{ | |
_next = next; | |
} | |
public async Task InvokeAsync(HttpContext context) | |
{ | |
var request = context.Request; | |
if (request.CanReadBody()) | |
{ | |
request.EnableBuffering(); | |
} | |
await _next(context); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment