Html.Forms 0.2.0

dotnet add package Html.Forms --version 0.2.0
NuGet\Install-Package Html.Forms -Version 0.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Html.Forms" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Html.Forms --version 0.2.0
#r "nuget: Html.Forms, 0.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Html.Forms as a Cake Addin
#addin nuget:?package=Html.Forms&version=0.2.0

// Install Html.Forms as a Cake Tool
#tool nuget:?package=Html.Forms&version=0.2.0

Html.Forms

License: MIT
Target framework: .NET Core 3.1+ or .NET 5+
Development status: Alpha

Framework for developing desktop applications with HTML GUI in .NET. Html.Forms app is .NET console application that hosts a minimal HTTP server and serves HTML pages on a local endpoint. User interacts with the app using any browser. Html.Forms uses HTML markup with minimal syntax extensions to define GUI and codebehind in C# or other .NET language to define logic. Apps run on any operating system supported by .NET.

Creating minimal app

Create new .NET Core/.NET console application project. Add index.html file that defines main page UI:

<html>
<head>
    <title>Html.Forms sample</title>
</head>
<body>
    <form>
        <h2>HTML Forms sample</h2>
        <p>Name: <input type="text" name="name" /></p>
        <p>Location: <input type="text" name="location" /></p>
        <p><input type="submit"/></p>
        <p><textarea readonly name="output"></textarea></p>
    </form>
</body>
</html>

Include index.html file as embedded resource in .csproj:

<ItemGroup>
    <None Remove="index.html" />
    <EmbeddedResource Include="index.html" />
</ItemGroup>

Add IndexPage class that defines main page codebehind:

using System;
using Html.Forms;

namespace HtmlFormsSample
{
    class IndexPage : Page
    {
        public IndexPage()
        {
            this.FileName = "index.html";
            this.Html = ReadFromResource(this.FileName);
        }

        protected override void OnLoad(LoadEventArgs args)
        {
            if (!this.HasField("name")) return;

            string name = this.GetFieldAs<string>("name");
            string location = this.GetFieldAs<string>("location");
            string output = "Hello, " + name + " from " + location + "!";
            this.SetField("output", output);
        }
    }
}

Modify Program.cs file to include framework startup code:

using System;
using Html.Forms;

namespace HtmlFormsSample
{
    class Program
    {
        static void Main()
        {
            Application app = new Application();

            using (app)
            {
                app.Name = "HtmlFormsSample";
                app.UrlPrefix = "/HtmlFormsSample/";
                app.AddPage(new IndexPage());
                app.RunInBackground();

                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }

            Console.WriteLine("Application exited.");
        }
    }
}

Now you can build and run the created app. Application.RunInBackground will attempt to automatically open HomepageUrl in default system browser, if it fails, you need to open browser manually. The example app demonstrates using the standard HTML Submit button to submit values of two text input fields and invoke Page.OnLoad method in codebehind class. The implementation of OnLoad reads submitted field values, produces output string and returns it to user as a value of <textarea> element.

Example Html.Forms application

Basic concepts

Html.Forms application is defined by creating an instance of the Html.Forms.Application class and adding one or more pages to it. Application renders pages on some local HTTP endpoint, by default it's http://localhost:8080/myapp/.

Page consists of the HTML file with UI definition and codebehind class derived from Html.Forms.Page. Page markup must include a single <form> element inside its body, and the contents of page UI must be placed inside that element. You can compose page UI from standard HTML elements and Html.Forms controls.

Supported HTML elements

HTML elements are defined statically in markup. Some elements, such as <h1>, are always rendered by framework in their unmodified form. Others, mainly <input> elements, are modified by markup rewriter to reflect values programmatically set from codebehind via Page.SetField method. The following elements are processed by rewriter:

  • <input type="text"/>
  • <input type="checkbox"/>
  • <input type="radio"/>
  • <textarea>
  • <select> and <option>
  • <div name="...">

<div> elements are only processed by rewriter if they have name attribute set and the form contains a value with the same name. Such elements are called dynamic blocks. The content of dynamic blocks, unlike for other elements, is not HTML-encoded before output. This enables inserting a dynamically generated HTML fragment into the rendered page.

Controls

Control is an object placed on a page that can dynamically render HTML using some logic defined in .NET code. Control classes inherit from Html.Forms.Control.

To use a control, put <dotnet-control> element into the page markup, setting its id attribute into some unique string. Then, in the constructor of the page class, create an instance of the control object, set its id to the same string as id attribute, and add it into the page using Page.AddControl method. The markup rewriter will replace the <dotnet-control> element with control's rendered markup when processing the page.

The following standard controls are provided in Html.Forms.Controls namespace:

  • ClockControl: displays current date and time
  • ComboBox: provides a drop-down list (<select>/<option> element) where list elements are dynamically defined in codebehind
  • NavigationPanel: renders a collection of hyperlinks defined dynamically in codebehind
  • ListView: renders a collection of items
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.0 203 11/28/2023
0.1.0 87 9/11/2023