Skip to content

Replace WebHostBuilder with HostBuilder pattern in MVC folder #62703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 14, 2025

Summary

This PR replaces all usages of WebHostBuilder with the modern HostBuilder and ConfigureWebHost pattern across the MVC folder, updating 24 files to follow current ASP.NET Core hosting best practices.

Changes Made

Pattern Transformation:

// Before
var host = new WebHostBuilder()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseStartup<Startup>()
    .UseKestrel()
    .Build();

// After  
using var host = new HostBuilder()
    .ConfigureWebHost(webHostBuilder =>
    {
        webHostBuilder
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .UseKestrel();
    })
    .Build();

Files Updated (24 total):

  • Samples (1): MvcSandbox/Startup.cs
  • Performance Benchmarks (3): BasicApi, BasicViews, RazorRendering
  • Test Website Program.cs (7): BasicWebSite, CorsWebSite, FormatterWebSite, RazorPagesWebSite, RazorWebSite, RoutingWebSite, SecurityWebSite, VersioningWebSite
  • Test Website Startup.cs (13): SimpleWebSite, FilesWebSite, ApiExplorerWebSite, ApplicationModelWebSite, ControllersFromServicesWebSite, ErrorPageMiddlewareWebSite, TagHelpersWebSite, XmlFormattersWebSite, HtmlGenerationWebSite (2 files), RazorBuildWebSite (2 files)

Key Changes Applied

Added using statements: using Microsoft.Extensions.Hosting;
Updated variable declarations: var hostusing var host
Renamed methods: CreateWebHostBuilder()CreateHost()
Changed return types: IWebHostBuilderIHostBuilder
Wrapped configuration: new WebHostBuilder()new HostBuilder().ConfigureWebHost(...)
Preserved all configurations: All original .UseKestrel(), .UseStartup<>(), .UseConfiguration(), .UseStaticWebAssets(), etc. methods maintained

Testing

  • All transformations follow the exact pattern specified in the requirements
  • Original functionality preserved - all configuration calls moved inside ConfigureWebHost lambda
  • Special configurations like .UseStaticWebAssets() in TagHelpersWebSite correctly handled
  • Complex configurations with local variables (like ConfigurationBuilder) properly maintained

Verification

  • ✅ 0 remaining new WebHostBuilder() usages in MVC folder
  • ✅ 24 files successfully transformed with new HostBuilder() pattern
  • ✅ All files compile with proper using statements and method signatures

This pull request was created as a result of the following prompt from Copilot chat.

Remove all usages of WebHostBuilder in the MVC folder and replace them with HostBuilder and ConfigureWebHost. Specifically, for each occurrence of:

Before:
var host = new WebHostBuilder()
.Configure(app => {
//...
})
.Build();

Change to:
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder => {
webHostBuilder
.Configure(app => {
//...
});
})
.Build();

Ensure all usages in the MVC folder follow this new pattern, updating variable declarations and method chains as needed.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

Copilot AI and others added 3 commits July 14, 2025 05:55
Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
…HostBuilder

Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
…tBuilder to HostBuilder

Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Replace WebHostBuilder with HostBuilder and ConfigureWebHost in MVC folder Replace WebHostBuilder with HostBuilder pattern in MVC folder Jul 14, 2025
@Copilot Copilot AI requested a review from BrennanConroy July 14, 2025 06:08
Copilot finished work on behalf of BrennanConroy July 14, 2025 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants