I have a startup.cs file and i want to pull configurations from XML file rather than appsetings.json
file. Is it possible with ASP.NET Core MVC?
Add the App. config file to your project. After creating a . NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.
The config. xml file consists of a series of XML elements. The Domain element is the top-level element, and all elements in the Domain are children of the Domain element. The Domain element includes child elements, such as the Server, Cluster, and Application elements.
We will use this XML configuration in our spring boot application. XML file is imported in configuration file using @ImportResource with @Configuration. In our main class we are using @SpringBootApplication annotation. @SpringBootApplication is the combination of @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.
It's similar to a pom.xml file but is defined globally or per user. Let's explore the elements we can configure in the settings.xml file. The main, settings element of the settings.xml file, can contain nine possible predefined child elements: 2.1.
There are different ways to set the values inside the configuration file and read their values, which are based on the defined keys. We define those values inside the configuration section, which might be needed to make it more secure. It can be some secret keys or the value, which should be received frequently.
We will create a REST web service with XML configuration. We will import our XML file in java configuration. We need to use @ImportResource with @Configuration in our spring boot application. We can keep our XML files in project classpath. Here we will create a spring boot web application that will work as REST web service.
If you want to use an appsettings.xml
instead you can refer to my blog article here.
Quote from the article:
To use XML files, you need to add this NuGet package: Microsoft.Extensions.Configuration.Xml. I added the 1.0.0 to my project.json and added a appsettings.xml file.
<configuration> <MySettings> <SomeSetting>Test</SomeSetting> <Another>true</Another> </MySettings> </configuration>
I then added it as part of configuration like this:
.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With