5

I am following an MS Tutorial that creates a minimal API in ASP.NET Core 8.0 and uses Endpoint Explorer in Visual Studio 2022 to generate an .http file to test the API endpoints.

Code for the model:

public class Todo
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public bool IsComplete { get; set; }
}

Code for database context:

using Microsoft.EntityFrameworkCore;

class TodoDb : DbContext
{
    public TodoDb(DbContextOptions<TodoDb> options)
    : base(options) { }

    public DbSet<Todo> Todos => Set<Todo>();
}

Code for API:

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

var app = builder.Build();

app.MapGet("/todoitems", async (TodoDb db) =>
        await db.Todos.ToListAsync());

app.MapGet("/todoitems/complete", async (TodoDb db) =>
        await db.Todos.Where(t => t.IsComplete).ToListAsync());

app.MapGet("/todoitems/{id}", async (int id, TodoDb db) =>
        await db.Todos.FindAsync(id)
            is Todo todo
                ? Results.Ok(todo)
                : Results.NotFound());

app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
    {
        db.Todos.Add(todo);
        await db.SaveChangesAsync();

        return Results.Created($"/todoitems/{todo.Id}", todo);
    });

app.MapPut("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
    {
        var todo = await db.Todos.FindAsync(id);

        if (todo is null) return Results.NotFound();

        todo.Name = inputTodo.Name;
        todo.IsComplete = inputTodo.IsComplete;

        await db.SaveChangesAsync();

        return Results.NoContent();
    });

app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
    {
        if (await db.Todos.FindAsync(id) is Todo todo)
        {
            db.Todos.Remove(todo);
            await db.SaveChangesAsync();
            return Results.NoContent();
        }

        return Results.NotFound();
    });

app.Run();

We are testing the posting data part here:

app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
{
   db.Todos.Add(todo);
   await db.SaveChangesAsync();

   return Results.Created($"/todoitems/{todo.Id}", todo);
 });

Then the tutorial instructs to use the Endpoints Explorer in VS2022 to generate a request (.http file), which I did.

enter image description here

Here's the code in the .http file:

@TodoApi_HostAddress = https://localhost:7284

Post {{TodoApi_HostAddress}}/todoitems
Content-Type: application/json

{
  "name":"walk dog",
  "isComplete":true
}

###

The problem I am having is that when I run the project click the Send Request link in the .http file, I am getting an error "Last request had errors. Click to send a new request." and no Response Pane appears in VS.

enter image description here

Error:

enter image description here

However, when I test this using Postman, I have no problem:

enter image description here

Any idea of what I've done wrong?

Thanks in advance and Merry Christmas

9
  • 1
    TBH was not able to repro. Can you please post full minimal reproducible example somewhere? Commented Dec 14, 2023 at 21:02
  • 1
    Still no repro. What VS version are you using? Have you tried updating it to the latest one? Commented Dec 14, 2023 at 21:43
  • 1
    The same. TBH out of ideas. You can try rebooting/running VS as admin/reinstalling VS but that's obviously is not guaranteed to work. Commented Dec 14, 2023 at 23:30
  • 1
    Thanks...Will give that a shot. Weird right? Merry Christmas! Commented Dec 14, 2023 at 23:47
  • 1
    Is there any other error message or output inside the VS output window? Commented Dec 15, 2023 at 9:05

1 Answer 1

2

After reboot problem was resolved.

Thanks all

Sign up to request clarification or add additional context in comments.

2 Comments

This drove me insane for a while. The restart worked!
For me restart is not help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.