Back to Practical Aspnetcore

WebApplicationBuilder - MVC

projects/minimal-hosting/web-application-builder-mvc/README.md

latest1.4 KB
Original Source

WebApplicationBuilder - MVC

In most cases using WebApplication isn't enough because you need to configure additional services to be used in your system. This is where WebApplicationBuilder comes. It allows you to configure services and other properties.

This example shows how to enable MVC using the minimalistic approach.

csharp
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

WebApplicationBuilder builder = WebApplication.CreateBuilder();
builder.Services.AddControllersWithViews();

var app = builder.Build();
app.MapControllers();
await app.RunAsync();

In contrast this is how it is done using Startup class (not all code included)

csharp
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute();
        });
    }
}

You can read the implementation of WebApplication here and its sibling WebApplicationBuilder here