This article is mainly for everyone to introduce the ASP. NET Core project configuration tutorial, with a certain reference value, interested in small partners can refer to
In this chapter, we will discuss the related configuration of the ASP. NET Core project. In Solution Explorer, you will see the Startup.cs file. If you have a previous version of ASP. NET work experience, you might want to see a Global.asax file where you can write code, which is a file that writes code that executes immediately when the program starts.
You might also want to see a Web. config file that contains all the configuration parameters that your application needs to perform.
in ASP. NET core, those files are gone and replaced by the Startup.cs file.
Startup.cs inside is a startup class file, and in that class you can configure your application and even configure your configuration resources.
Here is the default implementation code in the Startup.cs file:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Threading.Tasks; Using Microsoft.AspNetCore.Builder; Using Microsoft.AspNetCore.Hosting; Using Microsoft.AspNetCore.Http; Using Microsoft.Extensions.DependencyInjection; Using Microsoft.Extensions.Logging; Namespace Firstappdemo {public class-Startup {//This method gets called by the runtime.//Use this method to add Ser Vices to the container. For more information on what to configure your application,//Visit http://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, iloggerfactory loggerfactory) {loggerfactory. Addconsole (); if (env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); } app. Run (Async (context) = {await context. Response.writeasync ("HeLlo world! ");});}}
In the startup class, most of our work will be designed in two ways. The Configure method is where the HTTP processing pipeline is built.
This defines how the application responds to the request. Currently the app can only say "Hello world!" If we want the application to behave differently, we need to change the surrounding pipeline by adding extra code to this configure method.
For example, if we want to provide a static file for a index.html file, we will need to add some code to the Configure method.
You can also have an error page or route for the exception request of the ASP. These two scenarios also need to do some work in this configuration method.
In the startup class, you will also see the Configureservices () method. This helps you to configure the components of your application.
Now, we have a hard-coded string "Hello world!" To respond to each request. We don't want each request to be a hard-coded string, and we want to load the response string from some components.
Other components may load from the database, or from a Web service or a JSON file, regardless of where it is loaded from.
We're going to set up a scene so we don't have this hard-coded string.
In Solution Explorer, right-click your project node and select Add→new item.
In the left pane, select Installed→code, and then in the middle pane, select the JSON file. Name the file Appsetting.json, and click the Add button as above.
Let's add the following code to the appsettings.
{ "message": "Hello, world! This message was from the configuration file ... "}
Now we need to access this message from the Startup.cs file. Here is the implementation code for the Startup.cs file to read the above message from the JSON file.
Using Microsoft.AspNet.Builder; Using Microsoft.AspNet.Hosting; Using Microsoft.AspNet.Http; Using Microsoft.Extensions.DependencyInjection; Using Microsoft.Extensions.Configuration; Namespace Firstappdemo {public class Startup {public startup () {var builder = new Configurationbuilder (). Addjsonfile ("Appsettings.json"); Configuration = Builder. Build (); Public iconfiguration Configuration {get; set;} This method gets called by the runtime. Use this method to add services to the container. For more information on what to configure your application,//Visit http://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) {app. Useiisplatformhandler (); App. Run (Async (context) = {var msg = configuration["message"]; Await the context. Response.writeasync (msg); }); }//Entry point for the application. public static void Main (string[] args) =7gt; Webapplication.run<startup> (args); } }
Let's run the application now. Once you run the application, it produces the following output.