markdown写博客测试.md

对于像我这样没接触过core的人,坑还是比较多的,一些基础配置和以前差别很大,这里做下记录

一、Startup

1.注册服务

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            // services.AddTransient<IUser, User>();
            //services.AddSingleton<IUser>(new User());
            services.AddSingleton<IUser, User>();
             
            //services.AddScoped<>();//作用域注入
            services.AddMemoryCache(); //MemoryCache缓存注入
        }

2.配置HTTP请求管道

听起来很蒙,其实就是用于处理我们程序中的各种中间件,它必须接收一个IApplicationBuilder参数,我们可以手动补充IApplicationBuilder的Use扩展方法,将中间件加到Configure中,用于满足我们的需求。

// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

3.自定义配置文件

二、Program

1. core1.0和2.0对比

Program.cs这个文件包含了 ASP.NET Core 应用的 Main 方法,负责配置和启动应用程序。

  core1.0和2.0的program写法是不一样的(如下),但是本质是一样的,目的都是创建一个WebHostBuilder(WEB主机构建器,自己翻译的,呵呵),然后进行构建(Build),最后运行(Run)

.net core 1.0

三、使用日志

猜你喜欢

转载自www.cnblogs.com/yinchh/p/10392685.html