MWTech.Dotnet.WebApp.Helpers
1.0.0.4
dotnet add package MWTech.Dotnet.WebApp.Helpers --version 1.0.0.4
NuGet\Install-Package MWTech.Dotnet.WebApp.Helpers -Version 1.0.0.4
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="MWTech.Dotnet.WebApp.Helpers" Version="1.0.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MWTech.Dotnet.WebApp.Helpers" Version="1.0.0.4" />
<PackageReference Include="MWTech.Dotnet.WebApp.Helpers" />
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 MWTech.Dotnet.WebApp.Helpers --version 1.0.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MWTech.Dotnet.WebApp.Helpers, 1.0.0.4"
#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 MWTech.Dotnet.WebApp.Helpers@1.0.0.4
#: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=MWTech.Dotnet.WebApp.Helpers&version=1.0.0.4
#tool nuget:?package=MWTech.Dotnet.WebApp.Helpers&version=1.0.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MWTech.Dotnet.WebApp.Helpers
Extension methods and utilities for ASP.NET Core WebApplication builders and applications, simplifying common configuration and setup tasks.
Features
WebApplicationBuilder Extensions
- RegisterConfigSection: Bind and register configuration sections in DI with custom lifetimes
- Automatic validation of configuration existence
- Support for Singleton, Scoped, and Transient lifetimes
WebApplication Extensions
- RegisterFileServer: Configure static file serving with custom paths and directory browsing
- Flexible file provider setup
- Optional directory browsing
Installation
Copy the following files into your ASP.NET Core project:
Extensions/WebApplicationBuilderExtensions.csExtensions/WebApplicationExtensions.csGlobalUsings.cs
Usage
Registering Configuration Sections
using MWTech.Dotnet.WebApp.Helpers.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register a configuration section with default singleton lifetime
var myConfig = builder.RegisterConfigSection<MyConfig>();
// Or specify custom section name and lifetime
var dbConfig = builder.RegisterConfigSection<DatabaseConfig>(
"Database",
ServiceLifetime.Scoped
);
// Configuration is now available in DI
builder.Services.AddScoped<MyService>();
In appsettings.json:
{
"MyConfig": {
"Setting1": "value1",
"Setting2": "value2"
},
"Database": {
"ConnectionString": "Server=.;Database=MyDb;..."
}
}
Setting Up File Server
var app = builder.Build();
// Register file server for wwwroot with default settings
app.RegisterFileServer();
// Or specify custom root path and request path
app.RegisterFileServer(
rootPath: Path.Combine(Directory.GetCurrentDirectory(), "static"),
requestPath: "static",
enableDirectoryBrowsing: true
);
app.Run();
This serves files from:
- URL:
https://yourapp.com/static/ - Physical path:
YourApp/static/ - With directory browsing enabled
Extension Methods
WebApplicationBuilder.RegisterConfigSection<T>
public static T RegisterConfigSection<T>(
this WebApplicationBuilder builder,
string? sectionName = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
where T : class, new()
Parameters:
sectionName: Configuration section name (defaults to class name)lifetime: DI lifetime for the registered service
Throws:
InvalidOperationExceptionif configuration section doesn't exist
WebApplication.RegisterFileServer
public static WebApplication RegisterFileServer(
this WebApplication app,
string? rootPath = null,
string? requestPath = null,
bool enableDirectoryBrowsing = false)
Parameters:
rootPath: Physical directory path (defaults towwwroot)requestPath: URL path segment (defaults to root/)enableDirectoryBrowsing: Whether to show directory listings
Global Usings
Includes common ASP.NET Core usings:
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Http;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.FileProviders;
global using MWTech.Dotnet.Common.Helpers.Extensions;
Benefits
- Type Safety: Strongly-typed configuration binding
- Validation: Automatic configuration section validation
- Flexibility: Custom lifetimes and paths
- Convenience: Reduces boilerplate code
- Integration: Works seamlessly with existing ASP.NET Core patterns
Dependencies
- Microsoft.AspNetCore.App
- MWTech.Dotnet.Common.Helpers (for shared extensions)
License
MIT License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Microsoft.AspNetCore.OpenApi (>= 10.0.1)
- Microsoft.OpenApi (>= 2.3.11)
- Newtonsoft.Json (>= 13.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.