当前位置: 首页 > news >正文

动态网站 软件南和网站seo

动态网站 软件,南和网站seo,网站版面设计流程包括哪些,wordpress 提示在本文中,我们将通过组织一场小型音乐会(当然是在代码中)来了解 ASP .NET Core 中的环境变量。让我们从创建项目开始: dotnet new web --name Concert 并更新Program.cs: // replace this: app.MapGet("/"…

        在本文中,我们将通过组织一场小型音乐会(当然是在代码中)来了解 ASP .NET Core 中的环境变量。让我们从创建项目开始:

dotnet new web --name Concert

并更新Program.cs:

// replace this:
app.MapGet("/", () => "Hello World!");

// with this:
app.Logger.LogInformation("Playing {guitar} guitar", builder.Configuration["Guitar"]);

设置就是这么简单。现在让我们进行第一次声音检查:

cd Concert
dotnet run

# Produces:
#
# info: Concert[0]
#       Playing (null) guitar
# ...

好吧,这不会是一场特别精彩的音乐会null,对吧?让我们使用环境变量来解决这个问题:

export GUITAR=LesPaul && dotnet run && unset GUITAR
# Output: Playing LesPaul guitar

脚本以此结束,unset以确保我们在下一个实验之前有一个干净的环境。

配置
请注意,我们Guitar不是直接以环境变量的形式访问,而是通过使用IConfiguration访问器抽象来访问。默认情况下,ASP .NET Core访问器为我们提供了另外两种使用环境变量挑选吉他的方法:

ASPNETCORE_前缀变量:

export ASPNETCORE_GUITAR=Telecaster && dotnet run && unset ASPNETCORE_GUITAR 
# Output: Playing Telecaster Guitar

和DOTNET_前缀变量

export DOTNET_GUITAR=SG && dotnet run && unset DOTNET_GUITAR 
# Output: Playing SG guitar

如果你想知道如果我们同时使用两者会发生什么,答案如下:

export ASPNETCORE_GUITAR=Telecaster DOTNET_GUITAR=SG && dotnet run && unset ASPNETCORE_GUITAR DOTNET_GUITAR 
# Output: Playing SG guitar
# DOTNET_ prefixed variables take precedence

当然,IConfiguration不仅限于环境变量。appsettings.json还可以为我们提供配置值,所以让我们也在那里设置一把吉他:

{
  "Guitar" : "Stratocaster",
  ...
}

并进行一些实验:
export DOTNET_GUITAR=SG && dotnet run && unset DOTNET_GUITAR 
# Output: Playing Stratocaster guitar
# appsettings take precedence over prefixed environment variables

export GUITAR=LesPaul && dotnet run && unset GUITAR 
# Output: Playing LesPaul guitar
# Unprefixed environment variable takes precedence over appsettings

设置配置值的另一种方法是使用命令行参数。我们已经有了appsettings值,让我们也设置环境变量,提供命令行参数,看看会发生什么:

export GUITAR=LesPaul && dotnet run --Guitar=Firebird && unset GUITAR 
# Output: Playing Firebird guitar
# command line arguments take precedence over everything

我想强调的是,优先级和配置源列表并不是很神奇。这只是WebApplication.CreateBuilder(args)注册其配置源的一种方式。因此,如果我们扫描其内容,我们会在某处找到以下顺序的行:

configuration.AddJsonFile("appsettings.json");
configuration.AddJsonFile($"appsettings.{HostEnvironment.EnvironmentName}.json", optional: true);
configuration.AddEnvironmentVariables(prefix: "ASPNETCORE_");
configuration.AddEnvironmentVariables(prefix: "DOTNET_");
configuration.AddEnvironmentVariables();
configuration.AddCommandLine(args);

特殊环境变量
还有一些环境变量是单独使用的ASP .NET Core。为了先设置一个清晰的实验,我们Properties从项目中删除该文件夹。然后执行dotnet run将得到这样的日志:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
      
有相当多的主机变量【https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0#host-variables】。但ASPNETCORE_ENVIRONMENT和ASPNETCORE_URLS似乎是最重要的,研究它们应该能给我们足够的知识来流畅地操作任何其他托管变量。

export ASPNETCORE_URLS=http://+:5100 && dotnet run && unset ASPNETCORE_URLS
# Outputs: Now listening on: http://[::]:5100

export ASPNETCORE_ENVIRONMENT=Wembley && dotnet run && unset ASPNETCORE_ENVIRONMENT
# Outputs: Hosting environment: Wembley

请注意,宿主变量的行为可能与所有其他变量略有不同:

export ENVIRONMENT=Carnegie && dotnet run && unset ENVIRONMENT
# Outputs: Hosting environment: Production
# Unprefixed variable has no effect on the ASP .NET Core

确认【https://github.com/dotnet/aspnetcore/issues/55379#issuecomment-2081539608】这种差异是故意的。但并非每个主机变量都如此表现,只有“引导”变量:

export URLS=http://+:5800 && dotnet run && unset URLS
# Outputs: Now listening on: http://[::]:5800
# Here unprefixed variables not just affect ASP .NET Core
# but take precedence over a prefixed variable

章节和下划线
Microsoft.Extensions.Configuration框架也支持嵌套配置。我们先看看它如何与基于 json 的配置一起工作。

appsettings.json:

{
    "Band" : {
        "LeadGuitarist" : "Clapton"
    },
    ...
}

Program.cs:

app.Logger.LogInformation("{guitarist} playing {guitar}",
    builder.Configuration["Band:LeadGuitarist"],
    builder.Configuration["Guitar"]
);

//Output: Clapton playing Stratocaster

对于“嵌套”环境变量,使用双下划线:

export Band__LeadGuitarist=Hendrix && dotnet run && unset Band__LeadGuitarist
# Output: Hendrix playing Stratocaster

__请注意,使用双下划线是因为:对于某些 shell(包括 bash)来说,它不是有效的标识符。


Fluent 环境变量
您可能会注意到,这Band__LeadGuitarist是一个不符合典型 shell 约定的变量名。常规格式为:BAND_LEAD_GUITARIST。关于环境变量配置提供程序,有一个好消息:

export BAND__LEADGUITARIST=Hendrix && dotnet run && unset BAND__LEADGUITARIST
# Output: Hendrix playing Stratocaster
# So the provider is case incensitive

但这个好消息还不足以做到这一点:

export Band_LeadGuitarist=Gilmour && dotnet run && unset Band_LeadGuitarist
# Ouput: Clapton playing Stratocaster (a.k.a no effect)
# Single underscore doesn't work as separator

export Band__Lead_Guitarist=Gilmour && dotnet run && unset Band__Lead_Guitarist
# Ouput: Clapton playing Stratocaster (a.k.a no effect)
# You can not put an arbitrary underscore, too

但是,我们可以编写自己的配置提供程序。对于每个环境变量键,我们将注册键本身以及下划线的每个可能解释的键(作为分隔符和可跳过的部分):

public static IEnumerable<string> Keys(string rawKey)
{
      yield return rawKey;

      var parts = rawKey.Split("_").Where(p => p != "").ToArray();

      for (var i = 1; i < parts.Length; i++)
      {
            var beforeColon = parts.Take(i);
            var afterColon = parts.Skip(i);

            yield return String.Join("", beforeColon) + ":" + String.Join("", afterColon);
      }
}

提供程序将加载我们可以从环境变量中获取的所有配置键值对。

public class Provider : ConfigurationProvider
{
      public override void Load()
      {
            Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);

            foreach (DictionaryEntry environmentVariable in Environment.GetEnvironmentVariables())
            {
                var variableKey = (string)environmentVariable.Key;
                var value = (string?)environmentVariable.Value;

                foreach (var key in Keys(variableKey))
                {
                    Data.Add(key, value);
                }
            }
      }
}

我已经将提供程序制作成 nuget 包,因此您可以直接使用它:

dotnet add package Fluenv

using Fluenv;

...

builder.Configuration.AddFluentEnvironmentVariables();

然后几乎任何环境变量的命名都可以起作用,包括常规的命名:

export BAND_LEAD_GUITARIST=Gilmour && dotnet run && unset BAND_LEAD_GUITARIST
# Output: Gilmour playing Stratocaster

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。 


文章转载自:
http://prussianism.bbtn.cn
http://skidproof.bbtn.cn
http://privatism.bbtn.cn
http://awhirl.bbtn.cn
http://conservatively.bbtn.cn
http://pyrophobia.bbtn.cn
http://luna.bbtn.cn
http://biochrome.bbtn.cn
http://twae.bbtn.cn
http://remiges.bbtn.cn
http://theftuous.bbtn.cn
http://agatize.bbtn.cn
http://interuniversity.bbtn.cn
http://powwow.bbtn.cn
http://freshen.bbtn.cn
http://locker.bbtn.cn
http://plastiqueur.bbtn.cn
http://trimethadione.bbtn.cn
http://poikilothermal.bbtn.cn
http://glazer.bbtn.cn
http://lowerclassman.bbtn.cn
http://stalagmometer.bbtn.cn
http://buckbean.bbtn.cn
http://twas.bbtn.cn
http://landsraad.bbtn.cn
http://gitana.bbtn.cn
http://scintillate.bbtn.cn
http://bluesy.bbtn.cn
http://periodization.bbtn.cn
http://assuring.bbtn.cn
http://queerness.bbtn.cn
http://northerly.bbtn.cn
http://blackfoot.bbtn.cn
http://adipsia.bbtn.cn
http://urase.bbtn.cn
http://ginkgo.bbtn.cn
http://burstone.bbtn.cn
http://mlw.bbtn.cn
http://papistry.bbtn.cn
http://forfication.bbtn.cn
http://schoolmaid.bbtn.cn
http://tamable.bbtn.cn
http://rumina.bbtn.cn
http://finial.bbtn.cn
http://polycarbonate.bbtn.cn
http://splat.bbtn.cn
http://kasai.bbtn.cn
http://oeillade.bbtn.cn
http://bracelet.bbtn.cn
http://sbirro.bbtn.cn
http://loricate.bbtn.cn
http://shroud.bbtn.cn
http://monopolization.bbtn.cn
http://monozygotic.bbtn.cn
http://trowel.bbtn.cn
http://narration.bbtn.cn
http://adulation.bbtn.cn
http://cyo.bbtn.cn
http://soon.bbtn.cn
http://endopleura.bbtn.cn
http://effervescent.bbtn.cn
http://assured.bbtn.cn
http://salicional.bbtn.cn
http://tokomak.bbtn.cn
http://sequestra.bbtn.cn
http://auc.bbtn.cn
http://germ.bbtn.cn
http://hesiodic.bbtn.cn
http://candescent.bbtn.cn
http://staghound.bbtn.cn
http://documental.bbtn.cn
http://eldritch.bbtn.cn
http://blepharoplast.bbtn.cn
http://exsiccator.bbtn.cn
http://chronosphere.bbtn.cn
http://zomba.bbtn.cn
http://lateritious.bbtn.cn
http://detoxify.bbtn.cn
http://buoyage.bbtn.cn
http://bystander.bbtn.cn
http://benzophenone.bbtn.cn
http://muliebrity.bbtn.cn
http://surjection.bbtn.cn
http://chassid.bbtn.cn
http://recessive.bbtn.cn
http://periodontal.bbtn.cn
http://galvanometer.bbtn.cn
http://adoring.bbtn.cn
http://masterate.bbtn.cn
http://claimer.bbtn.cn
http://recolor.bbtn.cn
http://nebulae.bbtn.cn
http://replenisher.bbtn.cn
http://furrier.bbtn.cn
http://neptunism.bbtn.cn
http://kerala.bbtn.cn
http://superciliously.bbtn.cn
http://potstill.bbtn.cn
http://gravitino.bbtn.cn
http://thew.bbtn.cn
http://www.15wanjia.com/news/84812.html

相关文章:

  • 建设部网站公示钦州公租房摇号查询我是做推广的怎么找客户
  • 做网站建设出路在哪里百度网盘会员
  • 哈尔滨优化网站公司广东seo点击排名软件哪家好
  • 旅游类网站开发任务书网站seo优化是什么
  • 政府网站建设中存在的问题怎么联系百度客服人工服务
  • html个人网页设计代码成都seo优化
  • 太原市建设局网站首页培训后的收获和感想
  • 免费网站建设报价百度开户流程
  • 房产信息网站系统怎样利用互联网进行网络推广
  • 科技网站开发微信裂变营销软件
  • css 网站背景自己怎么开发app软件
  • p2p网站做牛宁海关键词优化怎么优化
  • 如何做链接武汉seo收费
  • 17做网站广州沙河地址每日新闻
  • 黑群晖可以做网站吗安顺seo
  • 天津做网站哪个公司好百度搜不干净的东西
  • 网站建设托管公司新闻头条最新消息
  • 做外贸自己建网站竞价托管外包
  • 电商网站建设电话教育机构
  • 网站一定要备案网络宣传方式
  • 给宝宝做衣服网站新浪体育最新消息
  • 校园网站设计毕业论文8000天津seo代理商
  • 上海定制app开发公司重庆百度seo整站优化
  • 网站电子商务类型如何宣传推广自己的产品
  • 大美工设计网站官网邯郸网站优化
  • 群辉服务器建设的网站单页网站怎么优化
  • wap购物网站源码外包网络推广公司推广网站
  • 深圳网站建设学校知乎seo
  • 网站标识网页界面设计
  • 杭州网站建设及推广新网域名注册查询