To add to what Richard has - correctly - said ...
In C# everything is part of a class, there is no "global code" or "global methods".
So when you write code like this:
namespace BookListRazor
12 {
13 public class Program
14 {
15 public static void Main(string[] args)
16 {
17 CreateHostBuilder(args).Build().Run();
18 }
19
20 public static IHostBuilder CreateHostBuilder(string[] args) =>
21 Host.CreateDefaultBuilder(args)
22 .ConfigureWebHostDefaults(webBuilder =>
23 {
24 webBuilder.UseStartup<Startup>();
25 });
26 }
27 }
28
29
30
31
32 var builder = WebApplication.CreateBuilder(args);
33
34
35 builder.Services.AddRazorPages();
The two close curly brackets on lines 26 and 27 close the Program class and the BookListRazor namespace respectively, and are outside of any class (and indeed namespace).
As such, the lines of code after them won't compile - they cannot be accessed at all so the compiler refuses to have anything to do with them.
In addition, even if you moved them into the class, they still wouldn't compile because with the exception of field or property definitions all executable code code must be contained in a method.
I assume that they should actually be a part of the CreateHostBuilder method, but since you have used a short form method definition you would need to change that to a curly bracket form instead:
namespace BookListRazor
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
...
}
}
}