-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
title: ASP.NET Core (Handler to Endpoint) | ||
--- | ||
import { Steps } from '@astrojs/starlight/components'; | ||
import NugetBadge from '../../../../../components/NugetBadge.tsx'; | ||
|
||
<NugetBadge name="Shiny.Mediator.AspNet" label="Shiny.Mediator.AspNet" /> | ||
|
||
Shiny Mediator now can map up your request handlers to ASP.NET Core endpoints using the minimal API. This | ||
saves you the effort of implementing the following boilerplate over and over and over again | ||
|
||
```csharp | ||
app.MapPost("/api/MyResultHandler", async (IMediator mediator, MyRequest request, CancellationToken ct) => | ||
{ | ||
return await mediator.Request(request, ct); | ||
}); | ||
``` | ||
|
||
The only thing that changes in this setup is the http method type, request type, and route. Now, all you have to do is | ||
following; | ||
|
||
<Steps> | ||
1. Install Shiny.Mediator.AspNet <NugetBadge name="Shiny.Mediator.AspNet" label="Shiny.Mediator.AspNet" /> to your ASP.NET Core project. | ||
|
||
2. On your request handler (void or result based), add `[MediatorHttpPost("MyRoute")]` or `[MediatorHttpPut("MyRoute")]` attribute to the handler. | ||
|
||
```csharp | ||
[MediatorHttpPost("MyRoute")] | ||
public class MyHandler : IRequestHandler<MyRequest, MyResult> | ||
{ | ||
public async Task<MyResult> Handle(MyRequest request, CancellationToken ct) | ||
{ | ||
return new MyResult(); | ||
} | ||
} | ||
``` | ||
|
||
:::note | ||
Currently - we only support `HttpPost` and `HttpPut` attributes. We will look into Get/Delete in the future | ||
::: | ||
|
||
3. In your host startup, register your handler (or use the source generator attributes). | ||
|
||
```csharp | ||
services.AddSingletonAsImplementedInterfaces<MyHandler>(); | ||
``` | ||
|
||
4. Again, in your host startup, add the following after your build your app. Here is a full-ish sample | ||
|
||
```csharp | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Services.AddShinyMediator(); | ||
services.AddSingletonAsImplementedInterfaces<MyHandler>(); | ||
var app = builder.Build(); | ||
|
||
app.UseShinyMediatorEndpointHandlers(builder.Services); | ||
app.Run(); | ||
``` | ||
|
||
</Steps> | ||
|
||
Here is a list of all the properties supported by the Http attributes | ||
|
||
```csharp | ||
public class MediatorHttpAttribute(string uriTemplate, HttpMethod httpMethod) : Attribute | ||
{ | ||
public string UriTemplate => uriTemplate; | ||
public HttpMethod Method => httpMethod; | ||
|
||
public bool RequiresAuthorization { get; set; } | ||
public string[]? AuthorizationPolicies { get; set; } | ||
public string? DisplayName { get; set; } | ||
public string? GroupName { get; set; } | ||
public string[]? Tags { get; set; } | ||
public string? Description { get; set; } | ||
public string? Summary { get; set; } | ||
public bool UseOpenApi { get; set; } = true; | ||
public string? CachePolicy { get; set; } | ||
public string? CorsPolicy { get; set; } | ||
public bool ExcludeFromDescription { get; set; } | ||
public string? RateLimitingPolicy { get; set; } | ||
public bool AllowAnonymous { get; set; } | ||
} | ||
``` | ||
|
||
:::warning | ||
It is best to register most handlers and middleware as `Scoped` on ASP.NET Core apps | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters