Member-only story

How to implement the Options Pattern in ASP.NET Core (Razor Pages)

Alex
6 min readNov 23, 2023

Since I have been getting more into developing ASP.NET Core applications I have been reading through various documentation regarding dependency injection, design patterns and HttpClient. So I thought I would write a tutorial on the Options Pattern which is a method of configuring application settings within a .NET application. It enables you to define strongly typed classes which your application can easily use and removes the need for hard coded values or accessing your configuration through square brackets and strings.

So to begin, create a new project in your IDE of type Web Application under the .NET Core framework. Since I am on a Mac I am having to get used to using the Rider IDE (which is great by the way and much better than Visual Studio for Mac in my opinion)

When you have successfully created the application navigate to the “appsettings.json” file, this file should contain all of your configuration which you will use on your current and other environments. By default the application will create a appsettings.development.json file where you could put in values that should only be used in your localhost i.e. passwords, identifiable user data.

Within the appsettings.json file (inside the outer object but outside the “logging” key ) add the below settings.

--

--

No responses yet