简体   繁体   English

在 ASP.net 核心应用程序的强类型配置类中使用 System.Uri

[英]Use System.Uri in strongly typed configuration class for ASP.net core app

In our AspNetCore 2.2 app, we make use of strongly typed classes for configuration sections, following the pattern:在我们的AspNetCore 2.2应用程序中,我们对配置部分使用强类型类,遵循以下模式:

public static IServiceCollection AddMyConfigSettings(this IServiceCollection services, IConfiguration configuration)
{
    MyConfigSettings myConfigSettings = new MyConfigSettings();
    configuration.Bind("MyConfig", myConfigSettings);
    services.AddSingleton(myConfigSettings);
    return services;
}

Now the MyConfigSettings class looks like this:现在MyConfigSettings类看起来像这样:

public class MyConfigSettings
{
    #pragma warning disable CA1056 // Uri properties should not be strings      
    public string HostUrl { get; set; }
}

Is there an easy way to use System.Uri instead of System.String as type for HostUrl, so that we don't need to suppress the Roslyn analyzer warning CA1056 ?有没有一种简单的方法可以使用System.Uri而不是System.String作为 HostUrl 的类型,这样我们就不需要抑制 Roslyn 分析器警告CA1056

Change the property from string to Uri .将属性从string更改为Uri

public class MyConfigSettings {        
    public Uri HostUrl { get; set; }
}

The bind should take care of the rest.绑定应该处理其余的。

You can also bind to the object graph like this您还可以像这样绑定到对象图

public static IServiceCollection AddMyConfigSettings(this IServiceCollection services, IConfiguration configuration) {
    MyConfigSettings myConfigSettings = configuration.GetSection("MyConfig").Get<MyConfigSettings>();
    services.AddSingleton(myConfigSettings);
    return services;
}

ConfigurationBinder.Get<T> binds and returns the specified type. ConfigurationBinder.Get<T>绑定并返回指定的类型。 Get<T> is more convenient than using Bind . Get<T>比使用Bind更方便。

The preceeding code shows how to use Get<T> with the your example, which allows the bound instance to be directly assigned to the property.前面的代码显示了如何在您的示例中使用Get<T> ,这允许将绑定实例直接分配给属性。

Reference Configuration in ASP.NET Core: Bind to an object graph ASP.NET Core 中的参考配置:绑定到对象图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 强类型的ASP.Net Core实时更新配置 - ASP.Net Core live update configuration on strongly typed 如何在ASP.NET Core中设置强类型配置? - How to set up strongly typed configuration in ASP.NET Core? 无法使用 Asp.Net Core 3 中的命名选项读取/绑定具有强类型 class 的配置设置 - Unable to read/bind configuration settings with strongly-typed class using Named Options in Asp.Net Core 3 .net 核心控制台应用程序强类型配置 - .net core console app strongly typed configuration 是否可以将强类型数组绑定为ASP.NET Core中的配置模型? - Is it possible to bind strongly typed arrays as configuration models in ASP.NET Core? 是否可以直接在ASP.NET 5(vNext)的类库中访问强类型的配置设置? - Accessing strongly typed configuration settings directly into class library in ASP.NET 5 (vNext)? ASP.NET 5:如何在更改时重新加载强类型配置 - ASP.NET 5: How to reload strongly typed configuration on change .net core控制台应用程序强类型配置 - .net core Console application strongly typed Configuration 来自appsettings.json的ASP.NET Core强类型选项 - ASP.NET Core strongly-typed options from appsettings.json ASP.NET Core 中的选项模式 - 如何将子选项作为 JSON 字符串返回(非强类型) - Options Pattern in ASP.NET Core - how to return sub-options as a JSON string (not strongly typed)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM