In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to set up the ASP.Net Core MVC development environment, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
In actual development, our systems tend to have at least two or more running environments, the most basic of which are development and operating environments, companies with complete systems, test environments, pre-release environments, and some custom environments, and so on. The configuration or some parameters used in these environments are certainly different, and it is impossible for us to prepare a code for an environment. We can automatically switch the environment through the environment configuration and some if judgment. Let's talk about it in detail below.
Let's feel it through the default .net Core MVC setting. The previous code can not be found, create a new empty. Net Core MVC project.
PS: IDE I already use VS2019
After it is built, we open the Startup.cs code file and look at the Configure method. I believe that some bloggers have found that the first line of code in it is an if, which means to judge whether the current environment is a development environment, and the literal meaning of the words is the same, so learning English well has a great buff bonus to the development program.
As for what is in if, I have already talked about it in the previous section and will not describe it any more.
From this, we can see that both the control environment and the judgment environment are carried out in the injected IHostingEnvironment interface object in the Configure method. Here I would like to say that the system provides several methods to judge the environment by default. Using the smart prompt of env.Is and VS, we can get the following four methods, as shown in the figure:
The IsDevelopment method is already known to determine whether it is a development environment or not.
IsProduction method to determine whether it is currently an operational (formal) environment
IsStaging method to determine whether it is a pre-run environment or not
The IsEnvironment method, which determines whether it is the current environment type based on the passed environment name (used to determine the custom environment)
Let's modify the code of the Configure method as follows:
Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {if (env.IsDevelopment ()) {app.UseDeveloperExceptionPage ();} app.Run (async (context) = > {context.Response.ContentType = "text/plain;charset=utf-8"; / / prevent WriteAsync method from outputting Chinese garbled if (env.IsDevelopment ()) {await context.Response.WriteAsync ("Development Environment", Encoding.UTF8) } else if (env.IsProduction ()) {await context.Response.WriteAsync ("operating environment", Encoding.UTF8);} else if (env.IsStaging ()) {await context.Response.WriteAsync ("pre-release environment", Encoding.UTF8) } else {await context.Response.WriteAsync ("Custom Environment", Encoding.UTF8);});}
Then F5 runs, and the browser will unexpectedly output: "Development Environment"
No problem, let's modify the compilation environment, change Debug to Release, and then generate, as shown in the figure:
Then generate the project. After the project is generated successfully, go to the project's bin/Release/netcoreapp2.2 directory, open CMD, and execute the dotnetUnit1.dll command, which will look like this, as shown in the figure:
We enter http://localhost:5000/ enter in the browser, no accident, will output: operating environment four big words.
The above process demonstrates the configuration and judgment of the most basic development environment and operation environment. Let's demonstrate a custom environment. We then modify the code to set a name for the current environment, as follows
Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {if (env.IsDevelopment ()) {app.UseDeveloperExceptionPage ();} env.EnvironmentName = "Cus"; / / set the custom environment name app.Run (async (context) = > {context.Response.ContentType = "text/plain;charset=utf-8" / / prevent WriteAsync method from outputting Chinese garbled if (env.IsDevelopment ()) {await context.Response.WriteAsync ("Development Environment", Encoding.UTF8);} else if (env.IsProduction ()) {await context.Response.WriteAsync ("operating Environment", Encoding.UTF8) } else if (env.IsStaging ()) {await context.Response.WriteAsync ("pre-release environment", Encoding.UTF8);} else {await context.Response.WriteAsync ("custom environment", Encoding.UTF8);}});}
F5 runs the project, and the browser unsurprisingly outputs: custom environment
If we want to output the pre-release environment, we just need to change the value of the EnvironmentName attribute to "Staging". Instead of demonstrating here, we will try it ourselves. The setting code is as follows:
Env.EnvironmentName = "Staging"; / / set to pre-release environment
The difference between sending to Staging and other values is that the system has an IsStaging method.
In order to more intuitively demonstrate the custom environment, we modify the else. The code after the change is as follows:
Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {if (env.IsDevelopment ()) {app.UseDeveloperExceptionPage ();} env.EnvironmentName = "Cus"; / / set the custom environment name app.Run (async (context) = > {context.Response.ContentType = "text/plain;charset=utf-8" / / prevent WriteAsync method from outputting Chinese garbled if (env.IsDevelopment ()) {await context.Response.WriteAsync ("Development Environment", Encoding.UTF8);} else if (env.IsProduction ()) {await context.Response.WriteAsync ("operating Environment", Encoding.UTF8) } else if (env.IsStaging ()) {await context.Response.WriteAsync ("pre-release Environment", Encoding.UTF8);} else {if (env.IsEnvironment ("Cus")) {await context.Response.WriteAsync ("Custom Environment: Cus", Encoding.UTF8) } else if (env.IsEnvironment ("Cus1")) {await context.Response.WriteAsync ("Custom Environment: Cus1", Encoding.UTF8);} else {await context.Response.WriteAsync ($"Custom Environment: {env.EnvironmentName}", Encoding.UTF8) }});}
The specific running effect is as expected, and the corresponding custom environment will be output.
However, in the actual development process, I do not recommend modifying the current environment in the code, and setting the corresponding environment through the environment variables of the project. The specific modification methods are as follows
1: click on Properties
2: open launchSettings.json
3: modify the value of ASPNETCORE_ENVIRONMENT.
Delete the code that modifies the EnvironmentName attribute, and change the configuration of launchSettings.json to Cus. What is the effect of running? the modified effect is shown in the figure.
Without any exception, the browser outputs a custom environment: Cus, change it to other values, enter the corresponding custom environment, modify it to Development is the development environment, Staging is the pre-operation environment, and Production is the operation environment.
Someone may be about to ask, you only modified the above ASPNETCORE_ENVIRONMENT, below there is an ASPNETCORE_ENVIRONMENT configuration, no modification, no matter what. Don't worry, I'll explain it right away.
Net Core MVC program, provides two kinds of hosting methods, one is hosted through IIS, the other is self-hosting. The environment we just modified only modified the IIS hosting mode, and VS defaults to IIS debugging, so it will be effective.
If you use self-managed mode to debug or release the running program, you can modify the configuration of the following Unit1 node. Of course, VS also provides debugging method, switch method, and click on the run mode, as shown in the figure:
Choose Unit1, that is, self-hosting mode, and then F5 runs, the browser output is still the development environment, although IIS is configured with Cus environment, but Unit1 self-hosting has not been modified, let's modify the Unit1 environment to try, here I modify to Cus100, and then select Unit1 debugging, F5 run, the browser output is
There's no problem.
If you find this modification troublesome, there is also a way to modify the interface.
We can see the corresponding configuration by right-clicking on the project-- > Properties-- > debugging, as shown in the figure:
Currently it is self-hosting mode. I changed it to Cus100. What is shown here is Cus100. Click the configuration file above, and you can modify the configuration of different hosting modes:
There is no demonstration here. The effect of modification is the same as that of directly modifying the launchSettings.json file.
PS: we have set both managed mode environments to the default Development, development environment.
Now that we have finished the configuration and switching of the environment, let's talk about automatically reading different configuration files according to different environments. Let's modify the code so that the input file is read from the appsettings.json configuration. The modified Startup class code is as follows:
Using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using System.Text; namespace Unit1 {public class Startup {private readonly IConfiguration _ Configuration / / the interface object public Startup (IConfiguration configuration) {this._Configuration = configuration;} / / This method gets called by the runtime of the injection or configuration file. Use this method to add services to the container. / / For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices (IServiceCollection services) {} / / This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {if (env.IsDevelopment ()) {app.UseDeveloperExceptionPage ();} / env.EnvironmentName = "Cus1"; / / set the custom environment name app.Run (async (context) = > {context.Response.ContentType = "text/plain" Charset=utf-8 "; / / prevent WriteAsync method from outputting Chinese garbled var msg = this._Configuration.GetValue (" hello "); await context.Response.WriteAsync (msg, Encoding.UTF8); / * if (env.IsDevelopment ()) {await context.Response.WriteAsync (" Development Environment ", Encoding.UTF8) } else if (env.IsProduction ()) {await context.Response.WriteAsync ("operating environment", Encoding.UTF8);} else if (env.IsStaging ()) {await context.Response.WriteAsync ("pre-release environment", Encoding.UTF8) } else {if (env.IsEnvironment ("Cus")) {await context.Response.WriteAsync ("Custom Environment: Cus", Encoding.UTF8) } else if (env.IsEnvironment ("Cus1")) {await context.Response.WriteAsync ("Custom Environment: Cus1", Encoding.UTF8) } else {await context.Response.WriteAsync ($"Custom Environment: {env.EnvironmentName}", Encoding.UTF8);}} * /});}
Then in the appsettings.json configuration, add a new line: "hello": "appsettings.json", as shown in the figure:
F5 execution, browser output appsettings.json, no problem
Then we click the arrow in front of the appsettings.json file, and we will find that there is also an appsettings.Development.json file inside, as shown in the figure:
Open the appsettings.Development.json file, add a hello configuration and set the value to appsettings.Development.json, just like configuring appsettings.json, as shown in the figure:
Then F 5 runs, and the browser outputs appsettings.Development.json. The reason here, which has been discussed in the previous chapter, is no longer described here.
Let's create a new appsettings.Cus.json file, and then add a hello configuration to it with a value of appsettings.Cus.json, as shown below:
Then change the running environment to Cus, modify the method above, and then run F5, and the browser will output appsettings.Cus.json ~
Thank you for reading this article carefully. I hope the article "how to set up the ASP.Net Core MVC Development Environment" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.