ASP. NET Core project configuration tutorial (6), asp. netcore

Source: Internet
Author: User

ASP. NET Core project configuration tutorial (6), asp. netcore

In this chapter, we will discuss the 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, you may want to see a global. asax file, where you can write code. It is a file that writes the code immediately when the program starts.

  • You may also want to see a web. config file that contains all the configuration parameters required for your application execution.
  • In ASP. NET Core, all the files are missing and replaced by the Startup. cs file.
  • Startup. cs is a Startup class file in which you can configure your applications or 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 how 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 the place where the HTTP processing pipeline is built.

  • This defines how an application responds to a request. Currently, this application can only say "Hello World !" If we want the application to have different behaviors, we need to add additional code to this Configure method to change the surrounding pipeline.
  • For example, if we want to provide a static file of the index.html file, we need to add some code in the Configure method.
  • You can also have an error page or an exception request route of Asp. Net Controller. You also need to do some work in this configuration method in these two scenarios.
  • In the startup class, you will also see the ConfigureServices () method. This helps you configure the components of your application.

Now, we have a hard-coded string "Hello World !" To respond to each request. We do not want each request to be a hardcoded string. We want to load the response string from some components.

  • Other components may load text from the database, or from a web service or a JSON file, no matter where it is loaded.
  • We will set a scenario so that we will not have this hardcoded string.

In Solution Explorer, right-click your project node and choose Add> New Item.

In the left-side pane, select Installed> Code, and then select a JSON file in the middle pane. Get the file named deleetting. json and click the Add button above.

Let's add the following code in AppSettings.

{  "message": "Hello, World! this message is from configuration file..." }

Now we need to access this message from the Startup. cs file. Here is the implementation code for reading the above message from the JSON file in the Startup. cs 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 how 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 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 generates the following output.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.