TodoApi in HTMX
provides methods for mapping endpoints related to Todo items in an ASP.NET Core application using HTMX. This simplifies the process of setting up routes for CRUD operations on Todo items.
- Maps the root endpoint to an Htmx Blazor component.
- Maps endpoints for loading, toggling, deleting, and adding Todo items.
- Uses in-memory storage for Todo items.
- ASP.NET Core application.
- Razor components for rendering UI.
- Ensure you have the necessary dependencies for Razor components and endpoint routing.
using TodoApi.Extensions;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Map the root endpoint to the Htmx Blazor component
app.MapToRootEndpoint();
// Map the Todo endpoints
app.MapTodoEndpoints();
app.Run();
Maps the root endpoint ("/"
) to an Htmx Blazor component.
public static void MapToRootEndpoint(this IEndpointRouteBuilder app)
- Parameters:
app
: TheIEndpointRouteBuilder
instance.
Maps the Todo endpoints for CRUD operations.
public static void MapTodoEndpoints(this IEndpointRouteBuilder app)
- Parameters:
app
: TheIEndpointRouteBuilder
instance.
- GET
/todos
: Loads all Todo items. - POST
/todos/{todoId}/toggle
: Toggles the completion status of a Todo item. - DELETE
/todos/{todoId}
: Deletes a Todo item. - POST
/todos
: Adds a new Todo item.
Represents a Todo item.
public class TodoItemModel
{
public Guid Id { get; set; }
public string Content { get; set; }
public bool IsCompleted { get; set; }
}
Represents a model for adding a new Todo item.
public class AddTodoItem
{
public string Content { get; set; }
}
Contributions are welcome! Please open an issue or submit a pull request.
Created by Gérôme Guillemin on April 1st, 2025
MIT