dotnet / dotnet/AspNetCore.Docs
Minimal API `MapGet(...).Produces(` and `.Produces<T>(` not available?
- Dominant language
- C#
- Stars
- 13.1k
- Forks
- 24.6k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 109
Description
### Description
Somehow I am not getting the Produces Methods on the OpenApi Endpoint for MapGet, but I dont know why 🤔 maybe someone can tell me, whats wrong?
if I would rely on `Task` for my Method signature or even with that above shown signature, I am getting that unmentioned in the docs "Return value would be ignored":
which would be quite bad, because I would like to somehow show my user that we got him Signed In successfully 😅 but I would not want to have to use the Identity Platform things itself just to do that with minimal api, should not be required, isnt it?
tryed to add any known to me namespaces that could be missing, while I do also have global usings in place, but wanted to make sure that I do not miss one:
not even Copilot is able to tell me whats up with that strange return value getting thrown away thing...
As this is just code following along a sample I found for WinUI oAuth as server side, here is the used code for this:
```csharp
using System.Net.Mime;
using System.Text;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Authentication;
namespace DevTKSS.MyManufacturerERP.WebApi.Apis;
public static RouteGroupBuilder MapAuthenticationEndpoints(this IEndpointRouteBuilder routes)
{
// Map the authentication endpoints
var connectgroup = routes.MapGroup("/connect")
.WithTags("Authentication");
connectgroup.MapGet("/authorize",Authorize)
.AllowAnonymous();
return connectgroup;
}
private static async Task> Authorize(HttpContext context)
{
var request = context.GetOpenIddictServerRequest();
if (request is null)
return TypedResults.BadRequest();
var identity = new ClaimsIdentity(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme, Claims.Name, Claims.Role);
identity.AddClaim(Claims.Subject, "dummy_user_id");
identity.AddClaim(Claims.Name, "Test User");
var principal = new ClaimsPrincipal(identity);
principal.SetScopes(Scopes.OpenId, Scopes.Profile, Scopes.Email);
await context.SignInAsync(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme, principal);
context.Response.Clear();
var template = await File.ReadAllTextAsync("page.html");
var code = Uri.EscapeDataString(context.GetOpenIddictServerResponse()!.Code!);
var state = Uri.EscapeDataString(context.GetOpenIddictServerResponse()!.State!);
var redirect = new UriBuilder(request.RedirectUri!)
{
Query = $"code={code}&state={state}"
}.Uri.ToString();
var html = string.Format(template, redirect);
context.Response.ContentType = "text/html; charset=utf-8";
return TypedResults.Content(html,MediaTypeNames.Text.Html,Encoding.UTF8,StatusCodes.Status200OK);
}
}
```
the used html template where the `string.Format` should do that replace:
```html
Authentication OK
window.location.href = "{0}";
body {{ font-family: sans-serif; text-align: center; margin-top: 50px; }}
Login completed!
If you do not see the app open automatically, click here:
Open the desktop app
You can now close this window.
```
this is my global usings content:
originally this was not having any return and directly in the Programm.cs 🤔
https://github.com/IlGalvo/WinUI3-OAuth2Manager-Sample/blob/a7a2cbc262665a99c07fcc51c9f42a4616f18eb2/Server/Server/Program.cs#L64-L91
I tryed to seperate it to own file and use the RouteGroupBuilder, but to build my response that would need the httpcontext I think. And reading the ms docs on the linked page below, we should prefer returning a BadRequest instead of throwing exceptions, but doing this in a non task seperated `MapGet(` like in that sample, this is giving me a red underline trying to make it return this.
Can anyone tell why I am not getting that Produces and advise how to set this up withouth this hell of added overhead the linked docs page does introduce us to do, just for showing the user this cute little html page?
### Page URL
https://learn.microsoft.com/de-de/aspnet/core/fundamentals/minimal-apis/responses?view=aspnetcore-9.0
### Content source URL
https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/responses.md
### Document ID
f0e5da68-cd54-ce74-1ff3-ebd7d9fd09a4
### Platform Id
03e8dae5-648b-cf4d-d50f-b00f0d8fc967
### Article author
@brunolins16
### Metadata
* ID: f0e5da68-cd54-ce74-1ff3-ebd7d9fd09a4
* PlatformId: 03e8dae5-648b-cf4d-d50f-b00f0d8fc967
* Service: **aspnet-core**
* Sub-service: **fundamentals**
[Related Issues](https://github.com/dotnet/AspNetCore.Docs/issues?q=is%3Aissue+is%3Aopen+f0e5da68-cd54-ce74-1ff3-ebd7d9fd09a4)
Contributor guide
Assessment
This issue has not been assessed yet.