一、前言

    这篇文章就是从能看到地方去学习Core,没有很深奥,也没有很难懂,现在我们开始吧。

二、构建项目,引发思考

    创建项目的步骤真的很简单,你要是不会,我真也没法了,我这是创建的MVC的项目。

接下来我们开始找不同,这个小学生也会的东西,我相信也难不到大家,这里我们主要说重点的不同:

1. 少了Content和Scripts文件夹,多了wwwroot;

2.少了配置文件web.config,多了appsettings.json;

3.少了App_Start文件夹,多了Program.cs和Startup.cs;

后面确实,实现了前面的功能,接下来引发下我们的思考,带着这些问题我们来学习Core:

配置文件:

1.为什么appsettings.json能默认成为配置文件?

2.怎么读取配置文件?

3.Global.asax如何在core中体现;

DI:

1.为什么全部能按照接口的方式进行注入?

2.怎么替换掉内置的DI?

MVC:

1.Core的请求和MVC的请求有什么不同?

2.Core的认证和授权怎么处理?

怀着这些问题我们来简单结合源码分析下,另外最后我们在上传一个简单的增删改查项目完成我们项目结束。

三、配置文件

   GitHub地址:https://github.com/aspnet/MetaPackages/tree/dev/src/Microsoft.AspNetCore

1.为什么appsettings.json能默认成为配置文件?

这个问题看下主要部分在这个地址下面:https://github.com/aspnet/MetaPackages/blob/dev/src/Microsoft.AspNetCore/WebHost.cs,我截取下主要代码;

      public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new WebHostBuilder(); if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
{
builder.UseContentRoot(Directory.GetCurrentDirectory());
}
if (args != null)
{
builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
} builder.UseKestrel((builderContext, options) =>
{
options.Configure(builderContext.Configuration.GetSection("Kestrel"));
})
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); if (env.IsDevelopment())
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
} config.AddEnvironmentVariables(); if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.ConfigureServices((hostingContext, services) =>
{
// Fallback
services.PostConfigure<HostFilteringOptions>(options =>
{
if (options.AllowedHosts == null || options.AllowedHosts.Count == )
{
// "AllowedHosts": "localhost;127.0.0.1;[::1]"
var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
// Fall back to "*" to disable.
options.AllowedHosts = (hosts?.Length > ? hosts : new[] { "*" });
}
});
// Change notification
services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration)); services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
})
.UseIISIntegration()
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
}); return builder;
}

2.怎么读取配置文件?

我主要提供了2种方式,这里前提是引入Microsoft.AspNetCore.All这个dll,这个里面很多我们需要的东西

从appsettings.json增加配置读取,有两种一个是更改配置以后立即生效,一种是重启以后才生效,具体看下代码:

  //配置如下
"JwtSettings": {
"Issuer": "http://localhost:52588",
"Audience": "http://localhost:52588",
"SecretKey": "1235455llllll"
},
"UserSetting": {
"Name": "wtzzzzz",
"Age":
}
//读取配置
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //读取JWT配置文件
services.Configure<JwtSettingsModel>(Configuration.GetSection("JwtSettings"));
//读取User配置文件
services.Configure<UserSettingModel>(Configuration.GetSection("UserSetting"));
}
//controller读取
//不支持实时更新
private readonly IOptions<JwtSettingsModel> _jwtConfiguration;
//支持实时更新
private readonly IOptionsSnapshot<UserSettingModel> _userConfiguration;
public HomeController(IOptions<JwtSettingsModel> jwtConfiguration,IOptionsSnapshot<UserSettingModel> userConfiguration)
{
_jwtConfiguration = jwtConfiguration;
_userConfiguration = userConfiguration;
}
public IActionResult Index()
{
ViewBag.Issuer = _jwtConfiguration.Value.Issuer;
ViewBag.Name = _userConfiguration.Value.Name;
ViewBag.PersonName =AppConfigurtaionHelper.GetAppSettings<PersonAppSettingModel>("PersonSetting").Area;
return View();
}

从外部引入配置文件,这个时候我们要做一件事情,将你新建的配置文件的属性设置为始终复制,至于这个原理是啥我不懂,有大神可以指点下,剩下的具体看代码吧;

{
"PersonSetting": {
"Area": "China"
}
} //外部的工具类
public class AppConfigurtaionHelper
{
public static T GetAppSettings<T>(string key) where T : class, new()
{
IConfiguration config = new ConfigurationBuilder()
.Add(new JsonConfigurationSource { Path = "/Config/setting.json", ReloadOnChange = true })
.Build(); var appconfig = new ServiceCollection()
.AddOptions()
.Configure<T>(config.GetSection(key))
.BuildServiceProvider()
.GetService<IOptions<T>>()
.Value; return appconfig;
}
}

3.Global.asax如何在core中体现;

        public void Configure(IApplicationBuilder app, IHostingEnvironment env,IApplicationLifetime application)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
} //类似于Application
//开始
application.ApplicationStarted.Register(() =>
{
Console.WriteLine("开始了注意");
}); //运行中
application.ApplicationStopping.Register(() =>
{
Console.WriteLine("运行中");
}); //结束
application.ApplicationStopped.Register(() =>
{
Console.WriteLine("结束了休息下");
}); app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

IApplicationLifetime这个实现替代在传统应用中Application中生命周期的位置,这个我在上面的代码中已有体现,使用命令的方式运行在控制台就可以看到;

四、DI

针对这个问题,我只能这么说为了为了提高大家,给大家实现一个容器,让大家玩的开心一下,让DI和IOC的思想深入内心,这点有点类似Java操作了,其实早就该这样了,已经让我们变大太懒了,哈哈,借用下蒋神的话,ASP.NET Core的核心是通过一个Server和若干注册的Middleware构成的管道,不论是管道自身的构建,还是Server和Middleware自身的实现,以及构建在这个管道的应用,都需要相应的服务提供支持,ASP.NET Core自身提供了一个DI容器来实现针对服务的注册和消费。换句话说,不只是ASP.NET Core底层框架使用的服务是由这个DI容器来注册和提供,应用级别的服务的注册和提供也需要以来这个DI容器,所以正如本文标题所说的——学习ASP.NET Core,你必须了解无处不在的“依赖注入”。

附上大神的地址:https://www.cnblogs.com/artech/p/dependency-injection-in-asp-net-core.html

不懂DI和IOC的我也附上腾飞大神的地址:https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html,

腾飞大神博客里面已经将我想要的说2个问题说的很明白了,我这里还是在稍稍说一下吧。

生命周期问题:

Transient: 每一次GetService都会创建一个新的实例

Scoped:每次Http请求只会产生一个实例;

Singleton:整个应用程序生命周期以内只创建一个实例

这个大神博客里面也有介绍,官方也有介绍,大家可以看下https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1

容器的替换:

public IServiceProvider ConfigureServices(
IServiceCollection services){
services.AddMvc();
// Add other framework services // Add Autofac
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterModule<DefaultModule>();
containerBuilder.Populate(services);
var container = containerBuilder.Build();
return new AutofacServiceProvider(container);
}

五、MVC

   1.Core的请求和MVC的请求有什么不同?

看图说话,主要设计不同主要体现在蒋神说的那句话,Core按照中间件的形式设计,最后全部注入到DI,这个时候我们考虑一个问题,我们想替换任何一个都将变得很容易,但是同样我们又会衍生另外一个问题,当我们服务之间存在依赖的时候,就需要按照顺序进行注入,这个问题需要重视一下,当然微软在设计上也考虑到这点,这里也是简单写个小demo,当你访问网站的时候就会发现网站按照顺序输出来了。MVC还是在IHttpMoudle和IHttpHandle上面做扩展或解耦,整体上本质是不变得,对这块不了解的可以推荐几篇博客给大家看看;推荐大龙龙MVC系列:http://www.cnblogs.com/edisonchou/p/3911558.html

            //注册中间件
app.Use(async (context,next)=>
{
await context.Response.WriteAsync("");
await next.Invoke();
}); app.Use(next =>
{
return (context) =>
{
context.Response.WriteAsync("");
return next(context);
};
}); app.Run(async (context) =>
{
await context.Response.WriteAsync("");
});

六、结束

    下一篇我们再说,Demo还没时间做好,另外强烈推荐大家去看官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/migration/http-modules?view=aspnetcore-2.1,真的比什么都管用!!!

从明面上学习ASP.NET Core的更多相关文章

  1. 学习ASP.NET Core Razor 编程系列十八——并发解决方案

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  2. 学习ASP.NET Core Blazor编程系列六——初始化数据

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  3. 学习ASP.NET Core Blazor编程系列十——路由(中)

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  4. 学习ASP.NET Core,你必须了解无处不在的“依赖注入”

    ASP.NET Core的核心是通过一个Server和若干注册的Middleware构成的管道,不论是管道自身的构建,还是Server和Middleware自身的实现,以及构建在这个管道的应用,都需要 ...

  5. 学习ASP.NET Core Razor 编程系列二——添加一个实体

    在Razor页面应用程序中添加一个实体 在本篇文章中,学习添加用于管理数据库中的书籍的实体类.通过实体框架(EF Core)使用这些类来处理数据库.EF Core是一个对象关系映射(ORM)框架,它简 ...

  6. 学习ASP.NET Core Razor 编程系列四——Asp.Net Core Razor列表模板页面

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  7. 学习ASP.NET Core Razor 编程系列五——Asp.Net Core Razor新建模板页面

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  8. 学习ASP.NET Core Razor 编程系列六——数据库初始化

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  9. 学习ASP.NET Core Razor 编程系列七——修改列表页面

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

随机推荐

  1. MySQLSource-Flume

    1. 自定义Source说明 实时监控MySQL,从MySQL中获取数据传输到HDFS或者其他存储框架,所以此时需要我们自己实现MySQLSource. 2. 自定义MySQLSource步骤 根据官 ...

  2. Java Socket通信实现私聊、群聊

    前言 闲言少叙,上代码! 代码编写 server服务端 /** * 服务端 */ public class Server { private static ServerSocket server = ...

  3. 第42章 发现(discovery) - Identity Server 4 中文文档(v1.0.0)

    可以在*https://baseaddress/.well-known/openid-configuration*找到发现文档.它包含有关IdentityServer的端点,密钥材料和功能的信息. 默 ...

  4. vue axios 批量删除 数组参数

    方法一:前端循环请求服务器端delete(id)方法 请问如何获得element-ui表格中的勾选项index,以实现批量删除功能 https://segmentfault.com/q/1010000 ...

  5. Hibernate框架笔记01_环境搭建_API_CRUD

    目录 1. Hibernate框架的概述 1.1 什么是框架 1.2 经典三层架构 1.3 Hibernate框架 2 Hibernate入门 2.1 下载Hibernate的开发包 2.2 创建项目 ...

  6. SpringMVC页面向Controller传参

    关于SpringMVC页面向Controller传参的问题,看了网上不少帖子,大多总结为以下几类: 1.直接把页面表单中相关元素的name属性对应的值作为Controller方法中的形参. 这个应该是 ...

  7. 怎么使用Fiddler进行抓包

    启动Fiddler,打开菜单栏中的 Tools > Fiddler Options,打开“Fiddler Options”对话框.      在Fiddler Options”对话框切换到“Co ...

  8. 章节九、4-ChromDriver介绍

    一.首先下载Chrom浏览器驱动,将驱动解压到存放火狐浏览器驱动文件路径中(请观看前面的章节) 1.进入该网址下载匹配本地浏览器版本的驱动 http://chromedriver.storage.go ...

  9. linux下可执行bin程序提示not found/no such file or directory/not executable

    我们经常在执行二进制bin程序时,会遇到提示not found/no such file or directory/not executable等错误信息,在什么情况下会出现这种问题呢,我们一起罗列下 ...

  10. MySQL5.7参数log_timestamps

    最近测试MySQL 5.7.21  Community Server这个版本的MySQL数据库时,发现其错误日志的时间跟系统当前时间不一致,后面检查发现日期时间格式都是UTC时间,查了一下相关资料,原 ...