Skip to content

Instantly share code, notes, and snippets.

@smaglio81
Created June 29, 2020 01:02
Show Gist options
  • Save smaglio81/24a8d750197a0cd964806dd2c84cf614 to your computer and use it in GitHub Desktop.
Save smaglio81/24a8d750197a0cd964806dd2c84cf614 to your computer and use it in GitHub Desktop.
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