In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "getting configuration information in ASP.Net Core MVC", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "getting configuration information in ASP.Net Core MVC".
In the previous new project, there is already a default appsettings.json file, and appsettings.json will be loaded into the project by default, as for why it will be loaded by default, we can analyze through the source code, VS2017 decompilation is not easy to use, F12 can not see the complete code, here I use ILSpy decompiler.
We find WebHost.CreateDefaultBuilder (args), that is, the CreateDefaultBuilder method, and see the following figure for the source code
The red box loads appsettings.json files by default. Careful bloggers may find that there is a get_EnvironmentName behind it, which is to get the current environment variables and load different appsettings.json files. In fact, after the project is newly built, there are two json files, appsettings.json and appsettings.Development.json, by default. In this way, .net Core will load both json configurations by default. If two json files have the same configuration, the later load will overwrite the previous load. It can also be understood that appsettings.json is a global configuration and appsettings.Development.json is a development configuration. We can do an experiment.
For our new empty MVC project, running the default access will only output a Hello World!, which is the default of the code, as shown in the figure
Now let's try to configure this string into the json file
Open appsettings.json and add a configuration, as shown in the figure
I configure a new welcome phrase, and then take this value. The value needs to be injected into an IConfiguration interface object to obtain configuration information. We inject the parameters of the Configure method in the Startup class, and then take the value, and modify the default output Hello wordwords. The code is shown in the figure.
Then F 5 runs, you can see the output of Hello .net Core on the page, no problem. Then let's configure the same configuration item in appsettings.Development.json and change the value, as shown in the figure
Then F5 runs the project, and the web page outputs Hello .net Core Development. It can be seen that .net Core will load the json file according to the current project environment. If there is the same, it will overwrite the configuration in appsettings.json, which can also be understood as a difference in priority. But it is not a difference in priority, but an overwrite.
We can also, for example, configure parameters from the console, we go to the project directory, run dotnet runWelcomeMsg= "AAA", and then access the http://localhost:5000/, output is AAA, so it can be said that the command line parameter priority is the highest.
The above is all the contents of the article "getting configuration Information in ASP.Net Core MVC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.