Questions tagged [minimal-apis]

Minimal APIs were introduced in ASP.NET Core 6 and are architected to create HTTP APIs with minimal dependencies. They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.

Minimal APIs were introduced in ASP.NET Core 6 and are architected to create HTTP APIs with minimal dependencies. They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.

Answers for many question can be found in the documentation.

236 questions
17
votes
6 answers

Minimal API in .NET 6 using multiple files

In .NET 6 it is possible to create minimal APIs: var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.MapGet("/products/{id}", (int id) => { return Results.Ok(); }) app.MapGet("/users/{id}", (int id) => { return…
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
16
votes
4 answers

MinimalAPI - Did you mean to register the "Body (Inferred)" parameter(s) as a Service or apply the [FromService] or [FromBody] attribute?

I created an asp.net core empty project and whenever I am trying to run my application it gives me the error shown below. I cannot even hit the end point as soon I hit play it gives the…
MarkCo
  • 810
  • 2
  • 12
  • 29
15
votes
5 answers

Add Swagger description to minimal .NET 6 APIs

I have a small project in .NET 6 that contains minimal APIs like that one app.MapGet("/clients", async (IClientRepository repo) => { var results = await repo.GetClientsAsync(); return…
Enrico
  • 3,592
  • 6
  • 45
  • 102
13
votes
1 answer

How to access DbContext in .NET 6 minimal API Program.cs

I am trying to call EF Core methods on application startup in my Program.cs file, using the .NET 6 minimal API template and get the following error: System.InvalidOperationException: 'Cannot resolve scoped service…
comp32
  • 201
  • 1
  • 5
  • 13
12
votes
1 answer

How to configure NewtonsoftJson with MinimalApi in .NET 6.0

I have net6.0 project with minimal api and I would like to use NetwtonsoftJson instead of built in System.Text.Json library for serialization and deserialization. At the moment I have this configurations for JsonOptions and that works as…
thug_
  • 893
  • 2
  • 11
  • 31
10
votes
3 answers

ASP.NET Minimal API How to Return/Download Files from URL

I'm working on minimal api, what I'm trying is when the user visits /download it immediately downloads my picture named add.png. But no matter what I try it doesn't work because I either get an empty page with only {} Is this possible? if so…
Teun
  • 161
  • 1
  • 1
  • 8
9
votes
1 answer

.NET 6 Minimal API and multipart/form-data

Using the .NET 6 Minimal API, I'm trying to handle multipart/form-data in the POST method. However, with the following code: app.MapPost("/tickets", async (IFreshdeskApiService s, [FromForm] CreateTicketDto dto) => await s.Add(dto)) …
9
votes
2 answers

How to return a json string with content type json in net 6 and minimal api?

If I have a json string (eg. read from a file) and my api returns as string, Postman will treat the response as text app.MapGet("/myapi", () => { var json = File.ReadAllText("file.json"); return json; }); So how can I force the content…
SandroRiz
  • 903
  • 4
  • 10
  • 18
8
votes
4 answers

How do I do file upload using ASP.NET Core 6 minimal api?

I want to create a simple file upload endpoint in ASP.NET Core 6 and thought it would be as easy as described here https://dotnetthoughts.net/handling-file-uploads-in-openapi-with-aspnet-core/. When I have an endpoint defined…
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
7
votes
0 answers

How can I "stream" search results from a minimal API using IAsyncEnumerable in c# using .NET 6?

Overview We're moving our database access to API calls and I need to return search results from a database as they're being found using that API. The plan was to use IAsyncEnumerable, but I'm having trouble dealing with the HttpClient buffer (at…
Dasta
  • 83
  • 5
6
votes
2 answers

Getting the current user in a minimal api

I am trying to refactor my api into a minimal api. Previously I've been using ControllerBase.HttpContext to get the user like this: var emial = HttpContext.User.FindFirstValue(ClaimTypes.Email); The method that I want to use for my endpoint mapping…
pușigreen
  • 91
  • 8
6
votes
2 answers

ASP.NET Core 6.0 - Minimal APIs: What parameters are available to Map methods for handling routes?

I'm new to Minimal API which is available in ASP.NET Core 6.0 and based on Microsoft's tutorials here and here, one can define a sample route for Get method like this: app.MapGet("/", () => "Hello World!"); For the Post method, the following code…
wiki
  • 1,877
  • 2
  • 31
  • 47
5
votes
1 answer

c# Minimal API .NET 6.0, how to get the client ip?

I use this link from microsoft https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0 to create my web api. after that, I add the database like this: var builder =…
5
votes
1 answer

How do I add authentication to an ASP.NET Core minimal API using Identity, but without using Azure?

I'm sure the answer to this must be obvious, as it's an obvious thing want to do, but I can't seem to find any guidance. I have an ASP.NET Core minimal API, and want to add authentication. I already have Identity set up in a different project, and…
4
votes
1 answer

.NET 7 minimal API AsParametersAttribute not mapping my request model as expected

I am trying to implement a simple generic minimal API MediatR method mapper (inspired by this video). I ran into an issue regarding mapping my request model when receiving data from body in the POST method with AsParametersAttribute. I've double…
Jorvat
  • 41
  • 3
1
2 3
15 16