相关文章推荐
霸气的跑步机  ·  FactoryBean threw ...·  11 月前    · 
威武的长颈鹿  ·  ORACLE SQL ...·  1 年前    · 
聪明的手电筒  ·  C# ...·  1 年前    · 
文质彬彬的蟠桃  ·  Python ...·  1 年前    · 
Throwing an error CS1022: Type or namespace definition, or end-of-file expected
What I have tried:
Program.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.AspNetCore.Hosting; 6 using Microsoft.Extensions.Configuration; 7 using Microsoft.Extensions.Hosting; 8 using Microsoft.Extensions.Logging; 11 namespace BookListRazor 13 public class Program 15 public static void Main(string[] args) 17 CreateHostBuilder(args).Build().Run(); 20 public static IHostBuilder CreateHostBuilder(string[] args) => 21 Host.CreateDefaultBuilder(args) 22 .ConfigureWebHostDefaults(webBuilder => 24 webBuilder.UseStartup<Startup>(); 25 }); 32 var builder = WebApplication.CreateBuilder(args); 34 // Add services to the container. 35 builder.Services.AddRazorPages(); 37 var app = builder.Build(); 39 // Configure the HTTP request pipeline. 40 if (!app.Environment.IsDevelopment()) 42 app.UseExceptionHandler( " /Error" ); 43 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. 44 app.UseHsts(); 47 app.UseHttpsRedirection(); 48 app.UseStaticFiles(); 50 app.UseRouting(); 52 app.UseAuthorization(); 54 app.MapRazorPages(); 56 app.Run(); At a guess, you're targeting .NET 6, and using the new top-level statements feature:
Top-level statements - C# tutorial | Microsoft Docs [ ^ ]
However, as the documentation says:
A file with top-level statements can also contain namespaces and type definitions, but they must come after the top-level statements .
You have added classes and methods before the top-level statements, which won't work.
You have also defined two entry-points: the top-level statements, and the Program.Main method.
It looks like you've tried to combine two lots of code by pasting the code from one application into the code from another, without actually understanding how either application is structured. 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 13 public class Program 15 public static void Main(string[] args) 17 CreateHostBuilder(args).Build().Run(); 20 public static IHostBuilder CreateHostBuilder(string[] args) => 21 Host.CreateDefaultBuilder(args) 22 .ConfigureWebHostDefaults(webBuilder => 24 webBuilder.UseStartup<Startup>(); 25 }); 32 var builder = WebApplication.CreateBuilder(args); 34 // Add services to the container. 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); // Add services to the container. builder.Services.AddRazorPages();
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •