Sphere10.Framework.Web.AspNetCore 3.2.0

dotnet add package Sphere10.Framework.Web.AspNetCore --version 3.2.0
                    
NuGet\Install-Package Sphere10.Framework.Web.AspNetCore -Version 3.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="Sphere10.Framework.Web.AspNetCore" Version="3.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sphere10.Framework.Web.AspNetCore" Version="3.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Sphere10.Framework.Web.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sphere10.Framework.Web.AspNetCore --version 3.2.0
                    
#r "nuget: Sphere10.Framework.Web.AspNetCore, 3.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.
#:package Sphere10.Framework.Web.AspNetCore@3.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sphere10.Framework.Web.AspNetCore&version=3.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Sphere10.Framework.Web.AspNetCore&version=3.2.0
                    
Install as a Cake Tool

Sphere10.Framework.Web.AspNetCore

This library is the shared ASP.NET Core integration for Sphere10 Framework. Use it to start and stop the Framework with an ASP.NET Core host and to connect logging, sessions, requests and middleware. The MVC and Blazor libraries build on this layer and add their controller and component APIs.

Features

Area APIs
Framework startup and shutdown AddSphere10Framework, UseSphere10Framework and StartSphere10Framework, the ModuleConfiguration that ends the Framework when the host stops, and GetAspNetCoreHost.
Logging AddSphere10FrameworkLogger, Sphere10LoggerProvider and MicrosoftExtensionsLoggerAdapter.
Session values SetJsonObject, TryGetJsonObject, GetJsonObject, GetOrCreateJsonObject and GetJsonObjectOrDefault.
Requests and context GetUserMessages, GetParameter<T>(Enum), PageParameterProcessor, GetUri, ToStringAsync and HttpContextEx.
HTML content IHtmlContentBuilder.ToHtmlContent(HtmlEncoder).
Networking Tools.Web.AspNetCore.ParseNetwork and the opt-in CloudflareConnectingIPMiddleware.

Installation

Reference the package from an ASP.NET Core project that targets .NET 8 or later:

dotnet add package Sphere10.Framework.Web.AspNetCore

Pass --version to match the other Sphere10.Framework packages in the application, and --source to restore from a local folder feed. The root README explains how to build and publish the packages, including to a private folder feed.

To build against the source instead, reference the project. From the repository root, these commands create the host for the examples below, beside the repository:

dotnet new web --name SharedWebDemo --output ../SharedWebDemo --framework net10.0
dotnet add ../SharedWebDemo/SharedWebDemo.csproj reference src/Sphere10.Framework.Web.AspNetCore/Sphere10.Framework.Web.AspNetCore.csproj

Quick start: framework lifecycle and sessions

Replace the generated Program.cs with:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSphere10Framework(framework => framework
	.AddModule<Sphere10.Framework.Web.AspNetCore.ModuleConfiguration>());
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession();

var app = builder.Build();
app.StartSphere10Framework();
app.UseSession();
app.MapGet("/", () => "Sphere10 Framework is running.");
app.MapGet("/visits", (HttpContext context) => {
	var counter = context.Session.GetOrCreateJsonObject<VisitCounter>("visits");
	counter.Count++;
	context.Session.SetJsonObject("visits", counter);
	return Results.Ok(counter);
});
app.Run();

public sealed class VisitCounter {
	public int Count { get; set; }
}

Run the host from the repository root:

dotnet run --project ../SharedWebDemo -p:BuildRevision=0

Open the address that the host prints, then visit /visits repeatedly in the same browser. The count lives in the session, which this example keeps in process memory, so it resets when the process restarts. A session read returns a deserialized copy, so call SetJsonObject again after you change the value. The session extensions serialize with Tools.Json, which uses Newtonsoft.Json.

How startup and shutdown work

AddSphere10Framework runs the Sphere10FrameworkBuilder callback and registers the modules that the callback adds in the host's service collection. Modules are never discovered automatically. Sphere10.Framework.Web.AspNetCore.ModuleConfiguration registers the initializer that calls Sphere10Framework.Instance.EndFramework() when the host raises ApplicationStopped, so without that module nothing ends the Framework when the host stops. Add Sphere10.Framework.Application.ModuleConfiguration as well when the host needs the product information and settings services. The Application guide describes modules and process tasks.

StartSphere10Framework must follow builder.Build() and precede app.Run(). It starts the pending configuration once, on the host's service provider, and the host keeps ownership of that provider. Sphere10Framework.Instance.GetAspNetCoreHost() then returns the host. For an IHostBuilder, UseSphere10Framework(configure) performs the same registration, and the start call is still required. Sphere10Framework.Instance is a process-wide singleton, so only one host in a process can run the Framework at a time.

Set Sphere10FrameworkOptions with WithOptions inside the AddSphere10Framework callback, because modules read them during registration. For example, Sphere10.Framework.Application.ModuleConfiguration reads EnsureSystemDataDirGloballyAccessible then. StartSphere10Framework takes no options and keeps the ones the callback set.

These calls manage the process-wide Framework. A Blazor application built with BuildBlazorApplication also has a scoped IApplication for each interactive circuit. That application initializes when its circuit opens and finalizes when the circuit closes. Both steps run the application's own tasks and the tasks registered under each loaded plugin's name. Prerendering and temporary disconnects run neither. Exiting the application ends only that circuit's application and does not stop the host. Use IApplication.Logger for application and screen diagnostics.

Domains

Every folder of Sphere10.Framework.Web.AspNetCore is a domain with its own README, which lists all of its public types.

Domain What it provides Source
Extensions Extension methods that start the Sphere10 Framework inside an ASP.NET Core host and add JSON session values, request helpers and per-request user messages. source
Lifecycle The internal initializer that ends the Sphere10 Framework when the ASP.NET Core host stops. source
Logging A Microsoft.Extensions.Logging provider and logger that forward ASP.NET Core log entries to a Sphere10 ILogger. source
Middleware ASP.NET Core middleware that replaces the connection's remote address with the client address Cloudflare reports. source
Processing Typed query-string parameter reading and a static accessor for the current HttpContext. source

Project root

Types declared in the project folder itself:

Type Kind Purpose
ModuleConfiguration class Framework module that registers the initializer which calls Sphere10Framework.Instance.EndFramework when the host raises ApplicationStopped.
Tools.Web.AspNetCore tools ParseNetwork parses IPv4 or IPv6 CIDR notation into a System.Net.IPNetwork and clears the host bits.

Send host logging to a Sphere10 logger

Add these imports at the top of the quick start's Program.cs and the two statements before builder.Build():

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sphere10.Framework;

builder.Logging.ClearProviders();
builder.Services.AddSphere10FrameworkLogger(new ConsoleLogger());

AddSphere10FrameworkLogger extends IServiceCollection, not ILoggingBuilder. It registers a Sphere10LoggerProvider, which gives each logging category a MicrosoftExtensionsLoggerAdapter over the supplied Sphere10.Framework.ILogger. Omit ClearProviders to keep the host's other providers as additional destinations.

Host level Sphere10 method Enabled by
Trace, Debug Debug LogOptions.DebugEnabled
Information Info LogOptions.InfoEnabled
Warning Warning LogOptions.WarningEnabled
Error, Critical Error LogOptions.ErrorEnabled

The adapter writes the text that the host's formatter produces and drops the category name and scopes. An Error or Critical entry with an exception goes to ILogger.Exception, and at the other levels the exception's text is appended to the message, with its stack trace when LogOptions.ExceptionDetailEnabled is set.

Request and network helpers

Network parsing

ParseNetwork parses IPv4 or IPv6 CIDR notation into a System.Net.IPNetwork and clears the host bits, so the address does not have to be normalized first:

using System.Net;

var network = Tools.Web.AspNetCore.ParseNetwork("192.0.2.123/24");
var baseAddress = network.BaseAddress; // 192.0.2.0
var includesClient = network.Contains(IPAddress.Parse("192.0.2.45")); // true

It throws ArgumentException for an invalid address and for a prefix length that is missing, not a number or outside the address's range.

Messages for the current request

GetUserMessages() returns a Collection<UserMessage> that is stored in HttpContext.Items. The collection lives only for the current request, so it is neither session state nor a cross-request notification queue. To try it, add the imports at the top of the quick start's Program.cs and the endpoint before app.Run():

using Microsoft.AspNetCore.Http;
using Sphere10.Framework.Web;
using Sphere10.Framework.Web.AspNetCore;

app.MapGet("/messages", (HttpContext context) => {
	context.GetUserMessages().Add(new UserMessage("Welcome.", UserMessageType.Information));
	return Results.Ok(context.GetUserMessages());
});

Typed query parameters

HttpRequest.GetParameter<T>(Enum) and PageParameterProcessor read a query-string value named by an enum member and parse it with Tools.Parser. The member's name is the parameter name unless a ParameterAttribute overrides it. ContainsParameter reports whether the query string carries the parameter, so call it before you read an optional parameter. GetParameter<T> throws SoftwareException when the query string lacks the value, and it throws FormatException when the value does not parse as T.

To try it, add the imports at the top of the quick start's Program.cs, the endpoint before app.Run() and the enum at the end of the file:

using Microsoft.AspNetCore.Http;
using Sphere10.Framework.Web.AspNetCore;
using Sphere10.Framework.Web.Processing;

app.MapGet("/search", (HttpRequest request) => {
	var page = request.ContainsParameter(SearchParameter.Page) ? request.GetParameter<int>(SearchParameter.Page) : 1;
	var text = request.ContainsParameter(SearchParameter.Text) ? request.GetParameter(SearchParameter.Text) : string.Empty;
	return Results.Ok(new { page, text });
});

public enum SearchParameter {
	[Parameter(Name = "page")]
	Page,

	[Parameter(Name = "q")]
	Text
}

A request for /search?page=2&q=reports returns the page number 2 and the text reports. GetParameter(Enum) without a type argument returns the value as a string.

Cloudflare client addresses

CloudflareConnectingIPMiddleware replaces HttpContext.Connection.RemoteIpAddress with the address in the cf-connecting-ip request header. The library does not register it, so add it early in the pipeline, before any middleware that reads the client address:

using Sphere10.Framework.Web.AspNetCore;

app.UseMiddleware<CloudflareConnectingIPMiddleware>();

The middleware trusts the header without checking the sender. Enable it only when the host accepts requests from Cloudflare alone. A header value that is not an IP address is ignored, and the connection keeps its own address.

Other helpers

API Behavior
HttpRequest.GetUri() Builds a Uri from the request's scheme, host, port, path and query. When the host carries no port, the Uri uses the scheme's default port, such as 443 for HTTPS.
HttpRequest.ToStringAsync() Formats the request line, headers and body as text. It reads the body to the end and leaves the stream open. It restores the position of a seekable body, so call EnableBuffering() first when another reader needs the body afterwards.
HttpContextEx.Current Returns the current HttpContext from a static accessor. Register the accessor with AddHttpContextAccessor() and pass it to HttpContextEx.Configure at startup first.
IHtmlContentBuilder.ToHtmlContent(HtmlEncoder) Renders the builder's content into one HtmlString.

The next additions to the quick start echo a request and configure HttpContextEx. Add the imports at the top of Program.cs, the registration before builder.Build() and the other statements before app.Run():

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Sphere10.Framework.Web.AspNetCore;

builder.Services.AddHttpContextAccessor();

HttpContextEx.Configure(app.Services.GetRequiredService<IHttpContextAccessor>());
app.MapPost("/echo", async (HttpRequest request) => {
	request.EnableBuffering();
	var dump = await request.ToStringAsync();
	return Results.Text($"{request.GetUri()}\n\n{dump}");
});

A POST to /echo returns the request's absolute URI, followed by its request line, headers and body. Code that has no HttpContext parameter can then read HttpContextEx.Current while a request is in progress.

Namespaces

The hosting, service and session extensions live in the Microsoft namespaces of the types they extend, so importing Sphere10.Framework.Web.AspNetCore alone does not bring them into scope.

Namespace APIs
Microsoft.Extensions.DependencyInjection AddSphere10Framework and AddSphere10FrameworkLogger
Microsoft.Extensions.Hosting UseSphere10Framework, StartSphere10Framework and ToHtmlContent
Microsoft.AspNetCore.Http The session JSON extensions
Sphere10.Framework.Web.AspNetCore GetUserMessages, GetParameter<T>, GetUri, ToStringAsync, GetAspNetCoreHost, PageParameterProcessor, HttpContextEx, CloudflareConnectingIPMiddleware, Sphere10LoggerProvider, MicrosoftExtensionsLoggerAdapter and ModuleConfiguration
Tools.Web Tools.Web.AspNetCore.ParseNetwork

Structure and dependencies

Folder or file Contents
Extensions/ Service, host, session, request, context and HTML-content extensions.
Lifecycle/ and ModuleConfiguration.cs The initializer that ends the Framework when the host stops.
Logging/ The Microsoft logging adapter and provider.
Processing/ PageParameterProcessor and HttpContextEx.
Middleware/ CloudflareConnectingIPMiddleware.
AspNetCoreTool.cs Tools.Web.AspNetCore.

The project targets net8.0, net9.0 and net10.0, and also net11.0 when a .NET 11 SDK builds it. It has a Microsoft.AspNetCore.App framework reference and project references to Sphere10.Framework.Application and the host-independent Sphere10.Framework.Web. It does not reference MVC, Blazor, Windows Forms or Drawing. The framework reference flows to referencing projects, so a plain Microsoft.NET.Sdk project such as the web test project can use these APIs.

Upgrading from the combined library

Releases up to 3.1.2 shipped the web models and the MVC helpers inside this package. They now live in two other packages, and this package keeps the hosting, HTTP, logging and middleware APIs under their existing namespaces.

Previous API Current package and namespace
Sitemap, navigation, message, HTML formatting and DataTables models Sphere10.Framework.Web, with the animation enums in Sphere10.Framework.Web.AnimateCss
ParameterAttribute Sphere10.Framework.Web, in the Sphere10.Framework.Web.Processing namespace
Controller and Razor helpers, forms, filters, routing, XmlResult and ImageResult Sphere10.Framework.Web.AspNetCore.MVC
Tools.Web.AspNetCore.ToSelectList Tools.Web.Mvc.ToSelectList in Sphere10.Framework.Web.AspNetCore.MVC

Build and verification

From the repository root:

dotnet build src/Sphere10.Framework.Web.AspNetCore/Sphere10.Framework.Web.AspNetCore.csproj -p:BuildRevision=0
dotnet test tests/Sphere10.Framework.Web.AspNetCore.Tests -p:BuildRevision=0

The shared web tests cover ParseNetwork normalization and rejection for IPv4 and IPv6, GetUri default ports, ToStringAsync, query parameters, the Cloudflare middleware and the logging adapter's exception forwarding, together with the MVC results and form scope. They do not cover the hosting or session extensions. Package validation builds and publishes an external host against the packed libraries and checks its responses over HTTP. -p:BuildRevision=0 stops a local build from allocating the next build number.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Sphere10.Framework.Web.AspNetCore:

Package Downloads
Sphere10.Framework.Web.AspNetCore.Blazor

Sphere10 Framework Blazor components, application shells, dialogs, wizards and data grids.

Sphere10.Framework.Web.AspNetCore.MVC

ASP.NET Core MVC extensions for Sphere10 Framework: controllers, Razor HTML helpers, Bootstrap forms, action filters, routing, and XML and image results.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 0 11/24/2026
3.2.0-ci.173.45375449609.1 0 11/24/2026
3.2.0-ci.171.45374063975.1 0 11/24/2026
3.2.0-ci.169.45350717665.1 0 11/24/2026
3.2.0-ci.168.45166918016.1 46 11/23/2026
3.2.0-ci.163.43664782937.1 79 11/15/2026
3.2.0-ci.160.43660265392.1 73 11/15/2026
3.2.0-ci.159.43537748063.1 82 11/14/2026
3.2.0-ci.158.43536665157.1 77 11/14/2026
3.2.0-ci.157.43350632870.1 79 11/13/2026
3.2.0-ci.156.43233095581.1 72 11/12/2026
3.1.2 157 9/9/2026
3.1.1 128 9/8/2026
3.1.0 121 9/7/2026
3.0.3 173 1/6/2026
3.0.2 154 1/2/2026
3.0.1 152 1/2/2026
3.0.0 144 1/2/2026