RazorDocs Search

Web

Namespaces

Runnable Web

The ForgeTrust.Runnable.Web package provides the bootstrapping logic for building ASP.NET Core applications using the Runnable module system. It sits on top of the compilation concepts defined in ForgeTrust.Runnable.Core.

Overview

The easiest way to get started is by using the WebApp static entry point. This provides a default setup that works for most applications.

await WebApp<MyRootModule>.RunAsync(args);

For more advanced use cases where you need to customize the startup lifecycle beyond what the options provide, you can extend WebStartup<TModule>.

Key Abstractions

WebApp

The primary entry point for web applications. It handles creating the internal startup class and running the application. It provides a generic overload WebApp<TModule> for standard usage and WebApp<TStartup, TModule> if you have a custom startup class.

IRunnableWebModule

Modules that want to participate in the web startup lifecycle should implement this interface. It extends IRunnableHostModule and adds web-specific hooks:

  • ConfigureWebOptions: Modify the global WebOptions (e.g., enable MVC, configure CORS).
  • ConfigureWebApplication: Register middleware using IApplicationBuilder (e.g., app.UseAuthentication()).
  • ConfigureEndpoints: Map endpoints using IEndpointRouteBuilder (e.g., endpoints.MapGet("/", ...)).

WebStartup

The base class for the application bootstrapping logic. While WebApp uses a generic version of this internally, you can extend it if you need deep customization of the host builder or service configuration logic.

Features

MVC and Controllers

Support for MVC approaches can be configured via WebOptions:

  • None: For pure Minimal APIs (default).
  • Controllers: For Web APIs using controllers (AddControllers).
  • ControllersWithViews: For traditional MVC apps with views.
  • Full: Full MVC support.

CORS

Built-in support for CORS configuration:

  • Enforced Origin Safety: When EnableCors is true, you MUST specify at least one origin in AllowedOrigins, unless running in Development with EnableAllOriginsInDevelopment enabled (the default). If AllowedOrigins is empty in production or when EnableAllOriginsInDevelopment is disabled, the application will throw a startup exception to prevent unintended security openness (verified by tests EmptyOrigins_WithEnableCors_ThrowsException and EnableAllOriginsInDevelopment_AllowsAnyOrigin).
  • Development Convenience: EnableAllOriginsInDevelopment (enabled by default) automatically allows any origin when the environment is Development, simplifying local testing without compromising production security.
  • Default Policy: Configures a policy named "DefaultCorsPolicy" (configurable) and automatically registers the CORS middleware.

Endpoint Routing

Modules can define their own endpoints, making it easy to slice features vertically ("Vertical Slice Architecture").

Configuration and Port Overrides

The web application supports standard ASP.NET Core configuration sources (command-line arguments, environment variables, and appsettings.json).

Port Overrides

You can override the application's listening port using several methods:

  1. Command-Line: Use --port (shortcut) or --urls.
    dotnet run -- --port 5001
    # OR
    dotnet run -- --urls "http://localhost:5001"
    
  2. Environment Variables: Set ASPNETCORE_URLS.
    export ASPNETCORE_URLS="http://localhost:5001"
    dotnet run
    
  3. App Settings: Configure urls in appsettings.json.
    {
      "urls": "http://localhost:5001"
    }
    

Note

The --port flag is a convenience shortcut that maps to http://localhost:{port};http://*:{port}. This ensures the application is accessible on all interfaces while logging a clickable localhost URL in the console. If both --port and --urls are provided, --port takes precedence.


📂 Back to Web List | 🏠 Back to Root

Type

WebOptions

Represents configuration options for the web application, including MVC, CORS, and static file settings.

Property

Mvc

MvcOptions Mvc { get; set; }

Gets or sets MVC-specific configuration options, such as support levels and custom MVC configuration.

Property

Cors

CorsOptions Cors { get; set; }

Gets or sets CORS configuration options for defining cross-origin resource sharing policies.

Property

StaticFiles

StaticFilesOptions StaticFiles { get; set; }

Gets or sets configuration options for serving static files within the web application.

Property

MapEndpoints

Action<IEndpointRouteBuilder>? MapEndpoints { get; set; }

Gets or sets an optional delegate to configure endpoint routing for the application.

Type

WebApp<TStartup, TModule>

Provides a static entry point for starting a web application with a custom startup class and root module.

Type Parameters

  • TStartupThe type of the custom startup class, inheriting from WebStartup{TModule}.
  • TModuleThe type of the root web module.
Method

RunAsync

Task RunAsync(string[] args, Action<WebOptions>? configureOptions = null)

Asynchronously runs the web application using the specified command-line arguments and optional option configuration.

Parameters

  • argsThe command-line arguments provided at application startup.
  • configureOptionsAn optional delegate to further customize WebOptions during startup.

Returns

A Task representing the asynchronous operation of running the web application.

Type

WebApp<TModule>

Provides a simplified static entry point for starting a web application using a default startup configuration.

Type Parameters

  • TModuleThe type of the root web module.
Method

RunAsync

Task RunAsync(string[] args, Action<WebOptions>? configureOptions = null)

Asynchronously runs the web application with a default startup using the specified command-line arguments and optional configuration.

Parameters

  • argsThe command-line arguments provided at application startup.
  • configureOptionsAn optional delegate to customize WebOptions during startup.

Returns

A Task representing the asynchronous operation of running the web application.

Type

MvcOptions

Represents configuration options for ASP.NET Core MVC services and features.

Property

Default

MvcOptions Default { get; }

Gets a new default instance of MvcOptions configured with MvcSupport.Controllers.

Property

MvcSupportLevel

MvcSupport MvcSupportLevel { get; init; }

Gets the level of MVC support to register (e.g., Controllers only, or Controllers with Views).

Property

ConfigureMvc

Action<IMvcBuilder>? ConfigureMvc { get; init; }

Gets an optional delegate for performing advanced configuration of the IMvcBuilder.

Enum

MvcSupport

Specifies the level of MVC feature support to enable in the web application.

Type

CorsOptions

Represents configuration options for Cross-Origin Resource Sharing (CORS) policies.

Property

EnableAllOriginsInDevelopment

bool EnableAllOriginsInDevelopment { get; set; }

Gets or sets a value indicating whether all origins are allowed when running in the development environment. Defaults to true.

Property

EnableCors

bool EnableCors { get; set; }

Gets or sets a value indicating whether CORS is enabled for the application. Defaults to false.

Property

AllowedOrigins

string[] AllowedOrigins { get; set; }

Gets or sets the collection of origins permitted to make cross-origin requests. Defaults to an empty array.

Property

PolicyName

string PolicyName { get; set; }

Gets or sets the name of the CORS policy to register. Defaults to "DefaultCorsPolicy".

Property

Default

CorsOptions Default { get; }

Gets a default instance of CorsOptions with default configuration settings.

Type

StaticFilesOptions

Represents configuration options for serving static files and web assets.

Property

Default

StaticFilesOptions Default { get; }

Gets a default instance of StaticFilesOptions with default configuration settings.

Property

EnableStaticFiles

bool EnableStaticFiles { get; set; }

Gets or sets a value indicating whether static files are enabled. This is automatically enabled when MvcSupport.ControllersWithViews or higher is used.

Property

EnableStaticWebAssets

bool EnableStaticWebAssets { get; set; }

Gets or sets a value indicating whether static web assets (from RCLs) are enabled. This is automatically enabled in the development environment.

Type

WebStartup<TModule>

Provides a base implementation for a web-based RunnableStartup{TModule} that handles MVC, CORS, and static file configuration based on registered IRunnableWebModule instances.

Type Parameters

  • TModuleThe root IRunnableWebModule for the application.
Method

WithOptions

WebStartup<TModule> WithOptions(Action<WebOptions>? configureOptions = null)

Registers an optional callback to customize WebOptions and enables fluent chaining.

Parameters

  • configureOptionsAn optional action invoked later when WebOptions are built to modify configuration.

Returns

The same WebStartup{TModule} instance to support fluent configuration.

Method

BuildModules

void BuildModules(StartupContext context)

Collects and caches all IRunnableWebModule instances found in the provided startup context. This method is idempotent.

Parameters

  • contextThe startup context whose dependencies and root module are inspected for web modules.

Remarks

This method is idempotent; subsequent calls have no effect once modules are built.

Method

BuildWebOptions

void BuildWebOptions(StartupContext context)

Initializes and caches WebOptions by applying configuration from discovered modules and the optional custom callback; enables static file support when MVC is configured for controllers with views.

Parameters

  • contextThe startup context used when invoking module and custom option configuration.

Remarks

This method is idempotent; subsequent calls have no effect once options are built.

Method

ConfigureServicesForAppType

void ConfigureServicesForAppType(StartupContext context, IServiceCollection services)

Configures services required for the web application: registers MVC application parts from the entry assembly and enabled web modules, and adds a CORS policy when CORS is enabled.

Parameters

  • contextStartup context providing environment information and the entry-point assembly.
  • servicesThe service collection to register MVC and CORS services into.

Exceptions

  • InvalidOperationExceptionThrown when CORS is enabled but no allowed origins are specified, except when all origins are explicitly allowed in development.
Method

ConfigureBuilderForAppType

IHostBuilder ConfigureBuilderForAppType(StartupContext context, IHostBuilder builder)

Configures the provided host builder with web host defaults and registers the application's web initialization pipeline.

Parameters

  • contextThe startup context used to collect modules and build web options.
  • builderThe host builder to configure.

Returns

The same IHostBuilder configured with web host defaults and the application's initialization pipeline.

Method

InitializeWebApplication

void InitializeWebApplication(StartupContext context, IApplicationBuilder app)

Configures the application's middleware pipeline and endpoint routing for the web application.

Parameters

  • contextThe startup context containing environment, entry point, and discovered modules used during configuration.
  • appThe application builder to configure (middleware, routing, CORS, endpoints, etc.).
Type

IRunnableWebModule

Defines a module that exposes web-specific configuration, endpoints, and middleware.

Method

ConfigureWebOptions

void ConfigureWebOptions(StartupContext context, WebOptions options)

Configures WebOptions for the application, such as MVC, CORS, and static files.

Parameters

  • contextThe startup context for the application.
  • optionsThe options to be configured.
Method

ConfigureEndpoints

void ConfigureEndpoints(StartupContext context, IEndpointRouteBuilder endpoints)

Allows the module to configure endpoint routes for the application.

Parameters

  • contextStartup context providing environment and configuration for the module.
  • endpointsEndpoint route builder used to map endpoints (routes, hubs, etc.).
Method

ConfigureWebApplication

void ConfigureWebApplication(StartupContext context, IApplicationBuilder app)

Configure the ASP.NET Core request pipeline for this module.

Parameters

  • contextStartup information and services available to the module during application initialization.
  • appThe application's request pipeline builder used to register middleware, routing, and other pipeline components.
Property

IncludeAsApplicationPart

bool IncludeAsApplicationPart { get; }

Gets a value indicating whether this module's assembly should be searched for MVC application parts (controllers, views, etc.). Defaults to false.