A typical web application separates setting from the App. In the ASP .NET Core web app allows encapsulation and separation of an interest of setting using the options pattern. It allows enables continuous integration while improving maintainability of an application.
This article describes as an introduce of the ASP.NET Core Options Pattern using globalization sample.
For globalization of ASP .NET Core samples using ‘.resx’ files are ideal pattern. These samples are created, edited, debugged, and continuously managed in Visual Studio, so users who aren’t using Visual Studio can use tools such as the online xml converter to get ‘.resx’ files.
If your development environment for productions that support many languages, I recommend that you do so, but if you create an application that supports a few languages such as just only Japanese and English, or lightweight web application such as PoC, demo or sample code. These apps globalize only few words, so It is useful that globalization using the options pattern without adopting full-scale globalization using ‘.resx’ files.

>>Code sample of this article


The way of separating ASP.NET settings from app hasn’t changed with the way of existing. Each method can use defined the setting value in the JSON file after registering the service on the host of the application.

In the case of ASP.NET MVC, if you register the service of the setting value in the host created by CreateBuilder, you can store the service received by the constructor of the controller in the variable defined globally in the scope of the controller, so can use settings variable in POST or GET method. You can use that variable in the processing of the request.


    public class SearchModel : PageModel
        private readonly ApplicationContext Context;
        public SearchModel(ILogger<IndexModel> logger, IConfiguration configuration)
            Context = new ApplicationContext();

        public void OnGet()
            ViewData["Message"] = Context.Languages.Count;
ⓘ As additional information

At that time, if the setting values ​​are separated using the options pattern, maintainable encapsulation can be performed. You can encapsulate by below.
1. The settings for individual methods or controllers, respectively.
2. At the level of “system” and “business” information such as logs and IP whitelists.
3. Semantics of settings.
4. more …

Often In the Domain Driven Design, the same thing is called by deferent name depending on boundary context on the scene in which it is used. When you purchase some product, what is displayed as “payment” in the member’s app is displayed as “sales” in the sales management system. It is OK that use deferent letteral objects on each individual systems if it mapped deferent labels by an individual system of a legacy system. It is important that using deferent settings value to handle same object of stateful middleware on deferent service in a modern system.

Thus, it can be said that the value of the settings handled in each boundary context with several same attributes are different when design settings elements.


In this article, the reason of using an implementation example of globalization is for describing the example of “Continuous integration of applications that read different setting values ​​while having the same attributes” to express the features of the options pattern.

In this article, the reason of using an implementation example of globalization is for describing the example of “Continuous integration of applications that read different setting values ​​while having the same attributes” to express the features of the options pattern.

At first, prepare Japanese and English sections in the appsettings.json, and it has brand name and the Pages segment. The segment has an action names as the ActionKey and a page titles as the Title.
Next, prepare a class with the same structure as the JSON file used for the configuration service registration for application builder.
Add the ApplicationContext.cs to the project, I recommend that adding the Models folder in the project at first, then input the ApplicationContext.cs into it.
One of the features of the ASP.NET options pattern is that defined const is name of the object and ignore it as structure of the object. Thus, this const can be used when get certain section of configuration service without hard coding ‘Japanese’ or ‘English’.
Then, Register target section of the builder configuration as the configuration service with the type same as certain section of appsettings.json.
Since the setting value registered as configuration service can be received by the argument of the constructor, store it in a variable at the global level scope of the class. It can use in individual method of the class, respectively.
ⓘ As additional information

I’m using the ViewData object because I needed to layer the objects when separating the options pattern. When using the options pattern as a mere encapsulation, the ViewBag object may be sufficient, as it is a set of parallel key / value combinations without grouped hierarchies.


No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *