How does ConfigureWebHostDefaults work

abbas izadipour 210 Reputation points
2023-06-24T08:41:29.9266667+00:00

In the following code snippet, I am having difficulty understanding where webBuilder came from. Assuming ConfigureWebHostDefaults is a method inside the parentheses, it should be a parameter being passed to the method. Within the parentheses, there is a function whose purpose I can understand. I also see that webBuilder is an IWebHostBuilder, but shouldn't we declare that variable somewhere before using it?

Here is the code snippet:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
              webBuilder.UseStartup<Startup>();
            });
Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-06-24T15:20:58.12+00:00

    This is basic c# 101.

    A common pattern is passing a callback (delegate) to method. The method defines the the supported delegate signature (parameters and return type). this pattern is so common, the Action<> and Func<> generics are defined. See:

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.generichostbuilderextensions.configurewebhostdefaults?view=aspnetcore-7.0

    In your sample code a lambda is used as the delegate.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.