In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the sample analysis of the configuration related to the ASP.NET Core project. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
We will discuss the relevant configuration of the ASP.NET Core project. In solution Explorer, you will see the Startup.cs file. If you have experience with previous versions of ASP.NET, you may want to see a global.asax file in which you can write code, which is a file that executes 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 to execute.
In ASP.NET Core, those files are gone and are replaced by Startup.cs files.
Inside Startup.cs is a startup class file in which you can configure your application or even 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 where the HTTP processing pipeline is built.
This defines how the application responds to requests. Currently, the app can only say "Hello World!" If we want the application to behave differently, we need to change the surrounding pipes by adding additional code to the Configure method.
For example, if we want to provide a static file of an index.html file, we will need to add some code to the Configure method.
You can also have an error page or the routing of abnormal requests from Asp.Net Controller; these two scenarios still need to do some work in this configuration method.
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 don't want every request to be a hard-coded string, we want to load the response string from some components.
Other components may load text from a database, or from a web service or a JSON file, no matter where it is loaded.
We will set up a scene so that we do not have the 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 shown in the screenshot above.
Let's add the following code to 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 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 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 (args);}}
Let's run the application now. Once you run the application, it produces the following output.
This is the end of this article on "sample Analysis of ASP.NET Core Project-related configurations". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.