ASP.NET Core四大部件

四大部件

(WebHost,Startup,launchSettings,wwwroot)

 

 

WebHost

 简单理解是一个socket,

https://www.cnblogs.com/neverc/p/7988226.html  (好)

 

 

Startup

 Startup.cs文件是ASP.NET Core的启动入口文件,想必尝试过OWIN开发的一定不会陌生。项目运行时,编译器会在程序集中自动查找Startup.cs文件读取启动配置。除了构造函数外,它可以定义Configure和ConfigureServices方法。

 

 

ConfigureServices  

给程序员添加一些服务的代码区

比如AddMvc查看mvc源码

        internal static void AddViewServices(IServiceCollection services)
        {
            DataProtectionServiceCollectionExtensions.AddDataProtection(services);
            AntiforgeryServiceCollectionExtensions.AddAntiforgery(services);
            EncoderServiceCollectionExtensions.AddWebEncoders(services);
            ServiceCollectionDescriptorExtensions.TryAddEnumerable(services, ServiceDescriptor.Transient<IConfigureOptions<MvcViewOptions>, MvcViewOptionsSetup>());
            ServiceCollectionDescriptorExtensions.TryAddEnumerable(services, ServiceDescriptor.Transient<IPostConfigureOptions<MvcViewOptions>, MvcViewOptionsConfigureCompatibilityOptions>());
            ServiceCollectionDescriptorExtensions.TryAddEnumerable(services, ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, TempDataMvcOptionsSetup>());
            ServiceCollectionDescriptorExtensions.TryAddSingleton<ICompositeViewEngine, CompositeViewEngine>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IActionResultExecutor<ViewResult>, ViewResultExecutor>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IActionResultExecutor<PartialViewResult>, PartialViewResultExecutor>(services);
            ServiceCollectionDescriptorExtensions.TryAddEnumerable(services, ServiceDescriptor.Transient<IControllerPropertyActivator, ViewDataDictionaryControllerPropertyActivator>());
            ServiceCollectionDescriptorExtensions.TryAddTransient<IHtmlHelper, HtmlHelper>(services);
            ServiceCollectionDescriptorExtensions.TryAddTransient(services, typeof(IHtmlHelper<>), typeof(HtmlHelper<>));
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IHtmlGenerator, DefaultHtmlGenerator>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<ExpressionTextCache>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IModelExpressionProvider, ModelExpressionProvider>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<ValidationHtmlAttributeProvider, DefaultValidationHtmlAttributeProvider>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IJsonHelper, JsonHelper>(services);
            ServiceCollectionDescriptorExtensions.TryAdd(services, ServiceDescriptor.Singleton<JsonOutputFormatter>(delegate (IServiceProvider serviceProvider) {
                return new JsonOutputFormatter(ServiceProviderServiceExtensions.GetRequiredService<IOptions<MvcJsonOptions>>(serviceProvider).get_Value().get_SerializerSettings(), ServiceProviderServiceExtensions.GetRequiredService<ArrayPool<char>>(serviceProvider));
            }));
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IViewComponentSelector, DefaultViewComponentSelector>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IViewComponentFactory, DefaultViewComponentFactory>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IViewComponentActivator, DefaultViewComponentActivator>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IViewComponentDescriptorCollectionProvider, DefaultViewComponentDescriptorCollectionProvider>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IActionResultExecutor<ViewComponentResult>, ViewComponentResultExecutor>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<ViewComponentInvokerCache>(services);
            ServiceCollectionDescriptorExtensions.TryAddTransient<IViewComponentDescriptorProvider, DefaultViewComponentDescriptorProvider>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<IViewComponentInvokerFactory, DefaultViewComponentInvokerFactory>(services);
            ServiceCollectionDescriptorExtensions.TryAddTransient<IViewComponentHelper, DefaultViewComponentHelper>(services);
            ServiceCollectionDescriptorExtensions.TryAddEnumerable(services, ServiceDescriptor.Transient<IApplicationModelProvider, TempDataApplicationModelProvider>());
            ServiceCollectionDescriptorExtensions.TryAddEnumerable(services, ServiceDescriptor.Transient<IApplicationModelProvider, ViewDataAttributeApplicationModelProvider>());
            ServiceCollectionDescriptorExtensions.TryAddSingleton<SaveTempDataFilter>(services);
            ServiceCollectionDescriptorExtensions.TryAddTransient<ControllerSaveTempDataPropertyFilter>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<ITempDataProvider, CookieTempDataProvider>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<ValidateAntiforgeryTokenAuthorizationFilter>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<AutoValidateAntiforgeryTokenAuthorizationFilter>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<ITempDataDictionaryFactory, TempDataDictionaryFactory>(services);
            ServiceCollectionDescriptorExtensions.TryAddSingleton<ArrayPool<ViewBufferValue>>(services, ArrayPool<ViewBufferValue>.get_Shared());
            ServiceCollectionDescriptorExtensions.TryAddScoped<IViewBufferScope, MemoryPoolViewBufferScope>(services);
        }

MVC大概注册了100多的服务。。。

 

Configure

 Configure 方法用于处理我们程序中的各种中间件,这些中间件决定了我们的应用程序将如何响应每一个 HTTP 请求。它必须接收一个IApplicationBuilder参数,我们可以手动补充IApplicationBuilder的Use扩展方法,将中间件加到Configure中,用于满足我们的需求。

 

 

<1> 这个方法用于在初始化的时候,将所有的中间件添加到 IApplicationBuilder 的componets数组中。

在Request请求发起的时候,按照添加顺序执行一次中间件,具体的我们下一节课说。

 

。。。。。。 这个方法配置的东西,只会在Request请求的过程中被调用 。。。。。。

<2> 这个方法的参数是不固定的,只要容器里面有,都可以实现依赖注入。。比如ILoggerFactory logger


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

环境的不同,程序的行为也不一样。。

 

 

 

launchSettings.json

 

解决问题:快速的做环境切换,方便调试和开发。

目的:让你的程序在多环境一样保持稳定。


1. 直接修改文件。
2. 使用vs面板。

 

 

wwwroot

 存放静态资源

 

 

 

代码下载

posted @ 2019-03-08 00:34  西伯利亚的狼  阅读(705)  评论(0编辑  收藏  举报