AspNet Core Web 应用程序的启动(有关 Program.cs类/ Startup.cs类 ) 当项目中干掉 Startup.cs 类如何设置启动 配置等等
有关怎么创建Core MVC/API 这里就不说了,前段时间的博客有说过:
1. 项目生成后会有如图所示两个类 Program类Startup类
2. Startup类 初始内容
- public void ConfigureServices(IServiceCollection services)
- {
- //运行时调用此方法。使用此方法向容器添加服务。
- }
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- //运行时调用此方法。使用此方法配置HTTP请求管道
- }
2.1 ConfigureServices 方法 使用 添加 MVC服务/EF/添加自定义服务
- public void ConfigureServices(IServiceCollection services)
- {
- //注入MVC的服务
- services.AddMvc();
- // 添加 EF 服务 可以添加多个 使用多个EF 多个库
- //services.AddEntityFrameworkSqlServer().AddDbContext<EFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
- // services.AddEntityFrameworkSqlServer().AddDbContext<EFLogDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerLog")));
- services.AddMvc();
- // 添加自定义服务 :详见 IServiceCollection
- }
2.2 Configure 方法的使用 MVC路由/静态文件/错误日志等
有关 IHostingEnvironment (https://msdn.microsoft.com/zh-cn/library/system.web.hosting.hostingenvironment.aspx)
- //重新定义 IHostingEnvironment
- public IHostingEnvironment HostingEnvironment { get; }
- //运行时调用此方法。使用此方法配置HTTP请求管道
- public void Configure(IApplicationBuilder app)
- {
- //判断当前的运行环境 是否是 Microsoft 如果是则返回true
- // 如果要判断其他的运行环境比如Linux 可以用 env.IsEnvironment("environmentname") 要验证的环境名称 忽略大小写
- if (HostingEnvironment.IsDevelopment())
- {
- //抓取错误信息 把错误信息生成 HTML
- //关于这个等写到关于错误处理的时候详细说明****************************
- app.UseDeveloperExceptionPage();
- }
- else
- {
- //自定义错误信息帮助页
- app.UseExceptionHandler("/Home/Error");
- }
- //已被重写
- //if (env.IsDevelopment())
- //{
- // app.UseDeveloperExceptionPage();
- //}
- //使用MVC默认路由
- app.UseMvcWithDefaultRoute();
- app.UseMvc(); //使用MVC的管道路径 可以在这里配置路由等操作
- //app.UseMvc(
- // routes =>
- // {
- // routes.MapRoute(
- // name: "User",
- // template: "{controller}/{action}/{id?}",
- // defaults: new { controller = "User", action = "Index" });
- // });
- //app.UseMvc(routes =>
- //{
- // routes.MapRoute(
- // name: "default",
- // template: "{controller}/{action=Index}/{id?}");
- //});
- }
2. Program类 初始内容
- public class Program
- {
- public static void Main(string[] args)
- {
- BuildWebHost(args).Run();
- }
- public static IWebHost BuildWebHost(string[] args) =>
- WebHost.CreateDefaultBuilder(args)
- .UseStartup<Startup>()
- .Build();
- }
2.1 实现不依赖 Startup 启动程序 可以直接在 Program 类中 构建 扩展,配置,配置 ,扩展,日志
- public class Program
- {
- public static IServiceCollection services { get; set; }
- public static IHostingEnvironment HostingEnvironment { get; set; }
- public static void Main(string[] args)
- {
- BuildWebHost(args).Run();
- }
- public static IWebHost BuildWebHost(string[] args) =>
- WebHost.CreateDefaultBuilder(args)
- //构建 扩展,配置,配置 ,扩展,日志,ILoggerFactory
- .ConfigureAppConfiguration((WebHostBuilderContext, config) =>
- {
- HostingEnvironment = WebHostBuilderContext.HostingEnvironment;
- })
- .ConfigureServices((IServiceCollection, services) =>
- {
- services.AddMvc();
- })
- .Configure(app =>
- {
- if (HostingEnvironment.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseExceptionHandler("/Error");
- }
- //使用MVC默认路由
- app.UseMvcWithDefaultRoute();
- //使用静态文件
- app.UseStaticFiles("");
- //配置路由
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller}/{action=Index}/{id?}");
- });
- })
- //被替换掉的启动项
- // .UseStartup<Startup>()
- .Build();
- }
不足之处,请大家指出相互学习