在这一部份内容中,我们来议论ASP.NET Core中的一个新功用:环境变量和启动设置,它将开辟过程当中的调试和测试变的越发简朴。我们只须要简朴的修正设置文件,就能够完成开辟、预演、生产环境的切换。
ASPNETCORE_ENVIRONMENT
ASP.NET Core掌握环境切换最中心的东西是“ASPNETCORE_ENVIRONMENT”环境变量,它直接掌握当前运用程序运转的环境范例。您能够经由过程在项目上右键菜单挑选“属性”选项,然后切换到“调试”标签来修正此环境变量。
此环境变量框架默许供应了三个值,固然您也能够定义别的的值:
Development(开辟)
Staging(预演)
Production(生产)
我们在Startup.cs文件中,能够运用响应的要领来掌握运用程序的行动。以下是建立示例程序时Startup.cs文件生成的默许代码:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); 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?}"); }); }
个中 IHostingEnvironment 范例的变量示意的是当前运用程序运转的环境,ASP.Net Core供应了四个扩大要领,用于检测 “ASPNETCORE_ENVIRONMENT”当前的值。
IsDevelopment()
IsStaging()
IsProduction()
IsEnvironment()
假如您须要搜检该运用程序是不是在特定环境中运转,能够运用 env.IsEnvironment("environmentname") ,该要领疏忽大小写(请不要运用 env.EnvironmentName == "Development" 来搜检环境)。
过上面的代码,我们能够晓得,假如当前是开辟环境,运用UseDeveloperExceptionPage()、UseBrowserLink()要领启用开辟环境的毛病页面和启用Visual Stuido中的Browser Link功用,这些功用都有利于我们在开辟过程当中调试程序;然则在生产环境中我们不愿望启用这些功用,而是将失足页面指向途径“/Home/Error”,给用户显现友爱的毛病界面。
launchSettings.json文件
ASP.Net Core包括一个launchSettings.json的新文件,您能够在项目中“Properties”文件夹中找到该文件:
此文件设置了Visual Studio能够启动的差别环境,以下是示例项目中launchSettings.json文件生成的默许代码:
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:22437/", "sslPort": 0 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "CoreWebApp": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:22438" } } }
在这里,有两个设置节点:“IIS Express”、“CoreWebApp”,这两个节点,离别对应Visual Stuido的最先调试按钮的下拉选项:
launchSettings.json 文件用于设置在 Visual Stuido 运转运用程序的环境。我们也能够增加节点,该节点称号会自动增加到 Visual Stuido 调试按钮的下拉选项中。
如今我们来细致说一下这些属性的细致信息:
{ "iisSettings": { "windowsAuthentication": false,//启用Windows身份验证 "anonymousAuthentication": true,//启用匿名身份验证 "iisExpress": { "applicationUrl": "http://localhost:22437/",//运用启动的Url途径。 "sslPort": 44355//启用SSL的端口 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "commandLineArgs": "", //通报敕令的参数 "workingDirectory": "", //设置敕令的事情目次 "launchBrowser": true, //是不是在浏览器中启动 "launchUrl": "1111", //在浏览器中启动的相对URL "environmentVariables": { //将环境变量设置为键/值对 "ASPNETCORE_ENVIRONMENT": "Development" } } } }
要猎取别的更多属性的细致信息,请转到此链接:http://json.schemastore.org/launchsettings 。
Environment 标签
经由过程这个标签,运用程序当依据当前运转的环境修正MVC视图的构造。在示例项目中_Layout.cshtml文件生成的默许代码:
<environment names="Development"> <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" rel="external nofollow" /> <link rel="stylesheet" href="~/css/site.css" rel="external nofollow" /> </environment> <environment names="Staging,Production"> <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css" asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css" rel="external nofollow" asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" /> <link rel="stylesheet" href="~/css/site.min.css" rel="external nofollow" asp-append-version="true" /> </environment>
在这个示例中,当在开辟形式下运转运用程序时,我们运用当地的Bootstrap文件和自定义css文件;然则假如在预演和生产环境中运转,我们则运用ASP.NET内容分发收集(CDN)上的文件副本和经由紧缩过的自定义款式。经由过程这类体式格局,我们能够进步运用程序的机能。
总结
在ASP.NET Core中,开辟者能够运用环境变量易如反掌掌握运用程序在差别的环境中的行动。运用这些功用,我们完成以下功用:
建立和运用自定义环境;
依据运用程序运转的环境启用或禁用运用程序部份功用;
运用 environment 标签修正当前环境中MVC视图。
以上就是ASP.NET Core中新功用--环境变量和启动设置的细致内容,更多请关注ki4网别的相干文章!