Coder Social home page Coder Social logo

blazorminimalapi's Introduction

.NET Minimal APIS, Razor Components, and HTMX

This is a demo on how to use Minimal APIs + Razor Components + HTMX to create dynamic server side rendered apps.

Using Minimal APIs and Razor components is possible because of a new class in .NET 8: RazorComponentResult

This returns an IResult after initializing a Razor component, making it possible to use Razor components in Minimal APIs.

Requirements

Requires .NET 8 RC1 installed.

Why do this?

Minimal APIs are a great way to organize your app. 1 endpoint corresponds to 1 method in a handler class. Simple and effective.

Razor components are great because you can re-use markup. But with Blazor WASM and Server, you are relying on a lot of magic and niche web technologies (web assembly and web sockets). With this setup, you are simply making normal HTTP requests and returning markup.

Pairing these 2 things together is an easy way to create maintainable, straightforward server side web apps. Add HTMX to this combination and you can build very powerful interfaces.

HTMX Usage

Partial page reload on navigation with hx-boost

The HTMX attribute hx-boost turns internal links into ajax calls. This speeds up site speed since there are no full page reloads. You can opt in and out of this behavior on a link by link basis.

This app uses hx-boost on the body tag, turning on this functionality for all links.

Get and Post requests with hx-get and hx-post

The HTMX attributes hx-get and hx-post sets up ajax calls to endpoints and swaps html on the page with what was returned.

This is useful for submitting forms, searching tables, and modifying the dom from a user interaction on the page.

Polling

The HTMX attribute hx-trigger can be used to poll the server. This can setup ajax calls to endpoints at an interval and swap html on the page with what was returned.

This is demonstrated on the /Components/Pages/Home.razor page in combination with the /Components/Pages/RandomNumber.razor component.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.