Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created April 19, 2021 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/0f696a6f209bbb57433a50c2e38d7ccf to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/0f696a6f209bbb57433a50c2e38d7ccf to your computer and use it in GitHub Desktop.
Exists to check if a configuration section exists
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration configuration;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
{
_logger = logger;
this.configuration = configuration;
}
public IActionResult Index()
{
var loggingSection = configuration.GetSection("Logging");
// To check if the section has a value or has children.
if (!loggingSection.Exists())
{
throw new System.Exception("logging section does not exist.");
}
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment