Development environment: Visual Studio 2022 Preview, .NET 6 , React 17. Create project
Press F5 to run debug, result
Write first custom middleware, with the presence of AI for Intelli Sense, AI suggesion works as I expected.
Write CustomLogger.cs
namespace ReactNET6
{
public class CustomLogger
{
private readonly RequestDelegate _next;
public CustomLogger(RequestDelegate next)
{
_next = next ?? throw new ArgumentNullException("next");
}
public async Task Invoke(HttpContext httpContext)
{
if (httpContext == null) throw new ArgumentNullException(nameof(httpContext));
await _next(httpContext);
}
}
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseCustomLogger(this IApplicationBuilder app)
{
return app.UseMiddleware<CustomLogger>();
}
}
}
Add 2 lines (line 1 and line 11) to file Program.cs
This is feature called "Top-level statements" in C# 9 https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#top-level-statements
We have Program.cs
with content
using ReactNET6;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseCustomLogger();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html"); ;
app.Run();
Publish
Result after publishing: C:\Users\donhuvy\source\repos\ReactNET6\bin\Release\net6.0\publish
, run file ReactNET6.exe
, go to http://localhost:5000 to see the result.