Skip to content

Commit

Permalink
Added API demo case for ASP.NET Core 3 + with context.Request.EnableB…
Browse files Browse the repository at this point in the history
…uffering(); fix (#559)

* Added api controller

* context.Request.EnableBuffering();
  • Loading branch information
304NotModified authored May 13, 2020
1 parent 9342736 commit cc23653
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace ASP.NET_Core_3___VS2019.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class LogTestsController : ControllerBase
{
private readonly ILogger<LogTestsController> _logger;

public LogTestsController(ILogger<LogTestsController> logger)
{
_logger = logger;
}

[HttpPost]
public async Task<ActionResult> TestLog([FromBody]string request)
{
try
{
throw new ApplicationException("Test log message");
}
catch (Exception ex)
{
_logger.LogError(ex, "Test error logging");
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
}
}
11 changes: 9 additions & 2 deletions examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -41,17 +42,23 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

// needed for ${aspnet-request-posted-body} with an API Controller. Must be before app.UseEndpoints
app.Use(async (context, next) => {
context.Request.EnableBuffering();
await next();
});

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});


}
}
}

0 comments on commit cc23653

Please sign in to comment.