Configuration of the ASP. NET Core Instance Tutorial

Source: Internet
Author: User
Tags hosting

ASP. NET core-Configuration

    1. ASP. NET core-Configuration

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 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, 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 Configurationbuilde R ().          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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.