这里有新鲜出炉的精品教程, 程序狗速度看过来!
ASP.NET
ASP.NET 是. NET FrameWork 的一部分, 是一项微软公司的技术, 是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术, 它可以在通过 HTTP 请求文档时再在 web 服务器上动态创建它们 指 Active Server Pages(动态服务器页面) , 运行于 IIS(Internet Information Server 服务, 是 Windows 开发的 Web 服务器)之中的程序
这篇文章主要为大家详细介绍了 ASP.NET Core 环境变量和启动设置的配置教程, 具有一定的参考价值, 感兴趣的小伙伴们可以参考一下
在这一部分内容中, 我们来讨论 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 ExpressCoreWebApp, 这两个节点, 分别对应 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 视图
来源: http://www.phperz.com/article/18/0310/352381.html