Overview

Minimal APIs in ASP.NET Core offer a streamlined approach for building HTTP APIs and web applications with less boilerplate code. They are designed to make the development process faster and more efficient, allowing developers to focus on the business logic rather than the underlying infrastructure. Minimal APIs are ideal for small to medium-sized projects and microservices, providing a simple yet powerful way to handle requests and responses.

Rizzy works with Minimal APIs by providing the ability to render both views and partial views. When Rizzy is added to your project on startup it adds a scoped service of type IRizzyService.

RizzyService

Methods

View Rendering

  • View<TComponent>(Action<RizzyComponentParameterBuilder<TComponent>> parameterBuilderAction): Renders a full view configuring the component parameters using a fluent builder.
  • View<TComponent>(object? data = null): Renders a full view using a specified Razor component. Optionally accepts dynamic data to pass to the component.
    • TComponent: The type of Razor component to render.
    • data: Dynamic data to pass to the component, optional.
  • View<TComponent>(Dictionary<string, object?> data): Renders a full view using a specified Razor component with explicitly provided data in dictionary form.
    • TComponent: The type of Razor component to render.
    • data: A dictionary containing the data to pass to the component.

Partial View Rendering

  • PartialView<TComponent>(Action<RizzyComponentParameterBuilder<TComponent>> parameterBuilderAction): Renders a partial view using a specified Razor component and a fluent parameter builder.
  • PartialView<TComponent>(object? data = null): Renders a partial view using a specified Razor component, suitable for inclusion in other views. Optionally accepts dynamic data.
    • TComponent: The type of Razor component to render as a partial view.
    • data: Dynamic data to pass to the component, optional.
  • PartialView<TComponent>(Dictionary<string, object?> data): Renders a partial view using a specified Razor component with explicitly provided data in dictionary form. Intended for component inclusion in other views.
    • TComponent: The type of Razor component to render as a partial view.
    • data: A dictionary containing the data to pass to the component.

Every View and PartialView overload also takes an optional ModelStateDictionary? modelState. Minimal APIs have no ModelState of their own, so pass one when the view’s form should show server-side errors through <RzModelStateProvider />.

Forms in minimal APIs

  • [SupplyParameterFromForm] properties in the rendered component are bound from the posted form, the same as with MVC controllers.
  • Endpoints that bind form data are protected by app.UseAntiforgery(); rizzy.js sends the token in a header on htmx requests. Endpoints that read the form without binding parameters need .WithMetadata(new RequireAntiforgeryTokenAttribute()) (or IAntiforgery.ValidateRequestAsync).
  • On .NET 11, Rizzy checks posts itself before supplying them to [SupplyParameterFromForm], using ASP.NET Core 11’s automatic CSRF protection: same-origin posts pass without a token, cross-site posts get 400 Bad Request. .DisableAntiforgery() opts an endpoint out. This check runs when the view renders, after your endpoint code, so endpoint code that changes data from the form still needs the protections above.
  • HttpContext.Response.Htmx() works in endpoints too, for example ValidationFailed() for a 422 response:
app.MapPost("/signup", (HttpContext http, IRizzyService rizzy, [FromForm] SignupModel model) =>
{
    var errors = new ModelStateDictionary();
    if (model.Username == "admin")
        errors.AddModelError("Model.Username", "That name is taken.");

    if (!errors.IsValid)
    {
        http.Response.Htmx().ValidationFailed();
        return rizzy.PartialView<SignupForm>(modelState: errors);
    }

    return rizzy.PartialView<SignupComplete>();
});

Note: Rizzy also features a source generator ([RizzyParameterize]) that adds a .Params() method to components, returning a strongly-typed dictionary which you can directly pass into these methods.

Utilities

  • CurrentActionUrl: A utility property that provides the current action method URL, useful for specifying form action targets within Razor Component views. Automatically derived from the current HTTP request.