CSharp Web API Concepts: Difference between revisions
Jump to navigation
Jump to search
Created page with "=Introduction= This pages is to capture some useful thing around the Web API. =Middleware= I was trying to match up my Typescript work with C# so I started off be looking a middleware. Not a difficult concept. So for .NET 8.0 for me it consisting of *Making and Extension *Applying the extension *Implementing the middleware Pretty straight forward <syntaxhighlight lang="cs"> // Create an extension public static IApplicationBuilder UseCustomMiddleware(this IApplication..." |
|||
Line 42: | Line 42: | ||
} | } | ||
} | } | ||
</syntaxhighlight> | |||
=Validation of Request= | |||
So I implement this with middleware in typescript too but watching youtube has led me to... | |||
<syntaxhighlight lang="cs"> | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 23:43, 21 November 2024
Introduction
This pages is to capture some useful thing around the Web API.
Middleware
I was trying to match up my Typescript work with C# so I started off be looking a middleware. Not a difficult concept. So for .NET 8.0 for me it consisting of
- Making and Extension
- Applying the extension
- Implementing the middleware
Pretty straight forward
// Create an extension
public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder app)
{
return app.UseMiddleware<CustomMiddleware>();
}
// Applying the extension
...
var app = builder.Build();
app.UseCustomMiddleware();
app.MapGet("/", (HttpContext context) =>
{
return "Hello World!";
});
...
// Implementing the middleware
public class CustomMiddleware
{
private readonly RequestDelegate _next;
public CustomMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
Console.WriteLine($"Request: {context.Request.Path}");
await _next(context);
}
}
Validation of Request
So I implement this with middleware in typescript too but watching youtube has led me to...