How can I retrieve AppSettings configuration back in Asp.Net MVC 6?

自古美人都是妖i 提交于 2019-12-01 04:04:55

You can get AppSettings in your FooService by injecting IOptions<AppSettings> DI service in it's constructor.

The IOptions<> interface is part of something called Options Model which is used for accessing POCO style settings(ex: your AppSettings) across your application. The calls like services.Configure<AppSettings>( and services.Configure<FacebookAuthenticationOptions>(options => in your above example, actually register DI services which in turn are used by a DI service called OptionsManager when resolving requests for IOptions<>.

Example:

public class FooService
{
    private readonly AppSettings _settings;

    public FooService(IOptions<AppSettings> options)
    {
        _settings = options.Options;
    }

    ....
    ....
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!