AspNet Core Web 应用程序的启动 当项目中 没有Startup.cs 类如何设置启动 配置等等

 

感叹: Core 16年6月1号 在中国宣布上线 到现在已经快经历两年时间了,目前版本已经到了2.0 就目前的前景来看,个人感觉 到2020年才可能有所起色,等到Core更成熟

个人看法:在.net这条路上总感觉后劲不足,市场好像越来越小了。不过学习还是要学的,毕竟很喜欢 c#

 

 

  (1)有关怎么创建Core MVC/API 这里就不说了,前段时间的博客中有说过:这里介绍的是有关 AspNetCore web 通用的基础知识,不针对单独的MVC/API介绍,也是我个人学习的结果,写出来希望和大家相互学习讨论,共同进步,希望在大家这里学到更多

  (2) 项目创建生成后我们会看到这样两个类 (Program/Startup)如图下图所示

                             

                             和我们的 ASP.NET MVC / ASP.NET Api  做比较 有关  Global.asax、FilterConfig.cs 和 RouteConfig.cs 等都被 Program.cs 和 Startup.cs两个类取而代之. 程序中 把Program.cs 作为 Web 应用程序的入口,程序启动的时候会调用 Startup.cs 类。

 Startup.cs 作用就是,对项目中用到的  静态文件,管道,服务,日志,路由,数据库连接,过滤器的注册  等  所有的有关程序的启动运行中用到的。、

                             传统的MVC/Api 使用的比较多的开发人员初步接触都会感到有点迷茫,这里我们对这两个类进行剖析。不足之处希望大家指出。

  (3) Startup  初始内容    

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

 

     3.1  ConfigureServices 方法 使用

                             

 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
        }

     3.2  Configure 方法的使用

 

//重新定义 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?}");
            //});
        }

 

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

 

  (4) 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();
    }

    4.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();
    }

 

 

 
    不足之处希望大家指出,相互交流学习
   
    
   

 

 

posted @ 2018-01-09 19:31  乐途  阅读(5838)  评论(1编辑  收藏  举报