自定义类ASP.Net 5 MVC 6中的应用程序设置

时间:2016-05-15 08:52:54

标签: c# asp.net asp.net-core-mvc

使用ASP.Net 5 MVC。看到这个问题跳了起来,但没有一个完整的答案。我想要做的是有一个能够访问AppSettings的帮助器类。我可以在控制器和视图中访问它,但还没有想出如何在我自己的自定义类中访问它。启动配置如此。

public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.

        var builder = new ConfigurationBuilder()
            .AddJsonFile("config.json")
            .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddOptions();
        services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
    }
.................
.................

5 个答案:

答案 0 :(得分:2)

所以在你的config.json文件中,假设你有以下设置

{
    "smtp": {
        "SenderEmail": "a@b.com",
        "SenderFrom": "Test User"
    }
}

然后在您的ConfigureServices方法中,您需要执行类似的操作

services.Configure<SmtpEmailSetting>(Configuration.GetSection("smtp"));

这是你的SmtpEmailSetting看起来像

public class SmtpEmailSetting
{
    public string SenderEmail { get; set; }
    public string SenderFrom { get; set; }
}

这就是您在任何服务或控制器中访问设置的方式

public class SendEmailService
{
    private readonly SmtpEmailSetting _smtpEmailSetting;

    public SendEmailService(IOptions<SmtpEmailSetting> smtpOptions )
    {
        _smtpEmailSetting = smtpOptions.Value;
    }

    public void SendEmail()
    {
        var fromEmail = _smtpEmailSetting.SenderEmail;
        var displayName = _smtpEmailSetting.SenderFrom;
    }
}

所以基本上你使用你的设置或选项(你喜欢调用的任何东西)应该在构造函数中用作IOptions<>类的泛型类型参数。希望它有所帮助

答案 1 :(得分:1)

要在自定义类中访问AppSettings属性,请将配置设置为静态实例,例如:

public static IConfigurationRoot Configuration { get; set; }

并在您的应用程序中使用您的AppSettings(对于connectionstring示例):

var connectionString = Startup.Configuration["Data:DefaultConnection:ConnectionString"];

答案 2 :(得分:1)

只是添加到adeel41的答案,这是正确的,并且效果很好,但对于我自己,我不想在使用依赖注入时拖动IOption对象。

所以我更喜欢做像

这样的事情
services.AddSingleton<ISmtpEmailSettings>(Configuration.GetSection("SmtpSettings").Get<SmtpEmailSettings>());

最重要的是将Get语句添加到GetSection以将JSON反序列化为对象。

答案 3 :(得分:0)

如果您在自己的类中需要它,将它传递给该类的构造函数或作为参数可能是正确的。例如;

{
    "user": {
        "name": "Ram",
        "age": 27
    }
}

通常情况下,您会从控制器中传递它。

这避免了一个全局变量,我认为这总是一件好事。

答案 4 :(得分:0)

在RC1的时候,我从Rick Strahl的this post中汲取灵感,效果很好。这与已经提出的其他方法非常相似。

这个问题只是为了更新我在RTM发布时的发现。似乎Configuration.GetSection(string topLevelKey)不再起作用,至少对我来说它总是返回null(即使配置源设置正确)。

经过一些搜索后,this other SO thread通过使用:

向我指出了正确的方向
// create empty config object
var smtpEmailSetting = new SmtpEmailSetting();

// fill it from configuration section
Configuration.GetSection("smtp").Bind(smtpEmailSetting);

// use it, e.g. by registering it into DI
services.Configure<SmtpEmailSetting>(smtpEmailSetting);

HTH