Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

656
Views
IAsyncActionFilter, intentando registrar el cuerpo de la solicitud, pero está vacío

Estoy tratando de configurar IAsyncActionFilter para registrar el cuerpo de la solicitud para algunas solicitudes de API. Pero cuando trato de leer el flujo del cuerpo siempre obtengo una cadena vacía.

Aquí está mi código: StringContent siempre es una cadena vacía, aunque haya un cuerpo json en las solicitudes posteriores.

 public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { ActionExecutedContext rContext = null; string stringContent = string.Empty; try { context.HttpContext.Request.EnableBuffering(); context.HttpContext.Request.Body.Position = 0; using (var reader = new StreamReader(context.HttpContext.Request.Body)) { stringContent = await reader.ReadToEndAsync(); context.HttpContext.Request.Body.Position = 0; } rContext = await next(); } catch (Exception) { throw; } }

No quiero usar middleware, porque necesito registrar solo algunos de los controladores.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El cuerpo de la solicitud ya ha sido leído por el enlazador de modelos MVC en el momento en que su IAsyncActionFilter ejecuta https://github.com/aspnet/Mvc/issues/5260 .

Como una solución rápida, puede agregar

 app.Use(next => context => { context.Request.EnableBuffering(); return next(context); });

en su startup.cs Configure () ANTES de su llamada UseEndpoints

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.Use(next => context => { context.Request.EnableBuffering(); return next(context); }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }

Por supuesto, esto habilitaría el almacenamiento en búfer del cuerpo de la solicitud para todas sus solicitudes, lo que puede no ser deseable. Si ese es el caso, tendría que agregar alguna lógica condicional al delegado EnableBuffering.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!