AspNet Core Web 应用程序的启动(有关 Program.cs类/ Startup.cs类 ) 当项目中干掉 Startup.cs 类如何设置启动 配置等等

有关怎么创建Core MVC/API 这里就不说了,前段时间的博客有说过:

1.  项目生成后会有如图所示两个类 Program类Startup类

2. Startup类  初始内容

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. //运行时调用此方法。使用此方法向容器添加服务。
  4. }
  5.  
  6. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  7. {
  8. //运行时调用此方法。使用此方法配置HTTP请求管道
  9. }

2.1  ConfigureServices 方法 使用  添加 MVC服务/EF/添加自定义服务

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. //注入MVC的服务
  4. services.AddMvc();
  5. // 添加 EF 服务 可以添加多个 使用多个EF 多个库
  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. // 添加自定义服务 :详见 IServiceCollection
  11. }

2.2   Configure 方法的使用  MVC路由/静态文件/错误日志等

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

  1. //重新定义 IHostingEnvironment
  2. public IHostingEnvironment HostingEnvironment { get; }
  3. //运行时调用此方法。使用此方法配置HTTP请求管道
  4. public void Configure(IApplicationBuilder app)
  5. {
  6. //判断当前的运行环境 是否是 Microsoft 如果是则返回true
  7. // 如果要判断其他的运行环境比如Linux 可以用 env.IsEnvironment("environmentname") 要验证的环境名称 忽略大小写
  8. if (HostingEnvironment.IsDevelopment())
  9. {
  10. //抓取错误信息 把错误信息生成 HTML
  11. //关于这个等写到关于错误处理的时候详细说明****************************
  12. app.UseDeveloperExceptionPage();
  13. }
  14. else
  15. {
  16. //自定义错误信息帮助页
  17. app.UseExceptionHandler("/Home/Error");
  18. }
  19. //已被重写
  20. //if (env.IsDevelopment())
  21. //{
  22. // app.UseDeveloperExceptionPage();
  23. //}
  24.  
  25. //使用MVC默认路由
  26. app.UseMvcWithDefaultRoute();
  27. app.UseMvc(); //使用MVC的管道路径 可以在这里配置路由等操作
  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类  初始内容

  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 启动程序      可以直接在 Program 类中 构建  扩展,配置,配置 ,扩展,日志

  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. //构建 扩展,配置,配置 ,扩展,日志,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. //使用MVC默认路由
  32. app.UseMvcWithDefaultRoute();
  33. //使用静态文件
  34. app.UseStaticFiles("");
  35. //配置路由
  36. app.UseMvc(routes =>
  37. {
  38. routes.MapRoute(
  39. name: "default",
  40. template: "{controller}/{action=Index}/{id?}");
  41. });
  42. })
  43. //被替换掉的启动项
  44. // .UseStartup<Startup>()
  45. .Build();
  46. }

不足之处,请大家指出相互学习

猜你喜欢

转载自www.cnblogs.com/Jeely/p/10959811.html