Start AspNet Core Web Application (about Program.cs class / Startup.cs class) when the project Startup.cs class how to get rid of the startup configuration, etc.

About how to create a Core MVC / API can not say here, some time ago the blog have said:

1. Project will generate two classes shown in FIG Startup Program class Class

2. Startup class initial content

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. // This method is called run-time. Use this method to add services to the container.
  4. }
  5.  
  6. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  7. {
  8. // This method is called run-time. Use this method to configure the HTTP request pipeline
  9. }

2.1 ConfigureServices method to add MVC service / EF / Add Custom Service

  1. publicvoidConfigureServices(IServiceCollection services)
  2. {
  3. // inject MVC service
  4. services.AddMvc();
  5. // Add the EF service you can add multiple use more of the plurality EF library
  6. //services.AddEntityFrameworkSqlServer().AddDbContext<EFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
  7. // services.AddEntityFrameworkSqlServer().AddDbContext<EFLogDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerLog")));
  8.  
  9. services.AddMvc();
  10. // add a custom service: See IServiceCollection
  11. }

2.2 Configure using the MVC routing method / static file / error logs, etc.

About IHostingEnvironment (https://msdn.microsoft.com/zh-cn/library/system.web.hosting.hostingenvironment.aspx)

  1. // redefine IHostingEnvironment
  2. public IHostingEnvironment HostingEnvironment { get; }
  3. // This method is called run-time. Use this method to configure the HTTP request pipeline
  4. public void Configure(IApplicationBuilder app)
  5. {
  6. // determine whether the current operating environment is Microsoft if it returns true
  7. // If you want to determine the name of the environment other operating environments such as Linux can env.IsEnvironment ( "environmentname") to validate ignore case
  8. if (HostingEnvironment.IsDevelopment())
  9. {
  10. // fetch error information to error messages generated HTML
  11. // writes about on this and other error when processing in detail ****************************
  12. app.UseDeveloperExceptionPage();
  13. }
  14. else
  15. {
  16. // custom error messages help page
  17. app.UseExceptionHandler("/Home/Error");
  18. }
  19. // has been rewritten
  20. //if (env.IsDevelopment())
  21. //{
  22. // app.UseDeveloperExceptionPage();
  23. //}
  24.  
  25. // Use the default MVC route
  26. app.UseMvcWithDefaultRoute();
  27. App . UseMvc (); // use the MVC pipeline path may configure the routing and other operations where
  28. //app.UseMvc(
  29. // routes =>
  30. // {
  31. // routes.MapRoute(
  32. // name: "User",
  33. // template: "{controller}/{action}/{id?}",
  34. // defaults: new { controller = "User", action = "Index" });
  35. // });
  36. //app.UseMvc(routes =>
  37. //{
  38. // routes.MapRoute(
  39. // name: "default",
  40. // template: "{controller}/{action=Index}/{id?}");
  41. //});
  42.  
  43. }

2. Program class initial content

  1. public class Program
  2. {
  3. public static void Main(string[] args)
  4. {
  5. BuildWebHost(args).Run();
  6. }
  7.  
  8. public static IWebHost BuildWebHost(string[] args) =>
  9. WebHost.CreateDefaultBuilder(args)
  10. .UseStartup<Startup>()
  11. .Build();
  12. }

2.1 Startup implementation does not rely initiator may be constructed directly in the Program class expanded configuration, the configuration, expansion, log

  1. public class Program
  2. {
  3. public static IServiceCollection services { get; set; }
  4. public static IHostingEnvironment HostingEnvironment { get; set; }
  5. public static void Main(string[] args)
  6. {
  7. BuildWebHost(args).Run();
  8. }
  9.  
  10. public static IWebHost BuildWebHost(string[] args) =>
  11. WebHost.CreateDefaultBuilder(args)
  12. // build extensions, configuration, configuration, expansion, logging, ILoggerFactory
  13. .ConfigureAppConfiguration((WebHostBuilderContext, config) =>
  14. {
  15. HostingEnvironment = WebHostBuilderContext.HostingEnvironment;
  16. })
  17. .ConfigureServices((IServiceCollection, services) =>
  18. {
  19. services.AddMvc();
  20. })
  21. .Configure(app =>
  22. {
  23. if (HostingEnvironment.IsDevelopment())
  24. {
  25. app.UseDeveloperExceptionPage();
  26. }
  27. else
  28. {
  29. app.UseExceptionHandler("/Error");
  30. }
  31. // Use the default MVC route
  32. app.UseMvcWithDefaultRoute();
  33. // use the static file
  34. app.UseStaticFiles("");
  35. // Configure routing
  36. app.UseMvc(routes =>
  37. {
  38. routes.MapRoute(
  39. name: "default",
  40. template: "{controller}/{action=Index}/{id?}");
  41. });
  42. })
  43. // be replaced startup items
  44. // .UseStartup<Startup>()
  45. .Build();
  46. }

Deficiencies, please point out to learn from each other

Guess you like

Origin www.cnblogs.com/Jeely/p/10959811.html