Asp.net core application analysis
I've heard about .Net core for a long time, but I just have time to learn and touch recently. As a part of .Net core, asp.net core can develop and run cross platform web applications on windows, MAC and Linux. Asp.net core is a significant reconstruction of asp.net. Due to the change of architecture, it is no longer based on system.web.dll. It has become a more compact and modular framework that can run on. Net core and complete. Net framework.
Microsoft official quick start tutorial:Get started with .NET in 10 minutes ,It contains the download address of the .Net core SDK。
After installation, you can view the version and help from the command prompt
//View version
dotnet --version
//View help
dotnet --help
//View template packages included in the initialization project help and framework
dotnet new --help
If you use visual studio 2017.3 preview and above, you can quickly experience the construction of development environment.
1. Create a simple asp.net core application
Viewing the generated project contains two class files program.cs and startup.cs
Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Seeing this static main function, I think of the first C# program in my life -「Hello World!」, here is the entry of the program!
2. Initialization of host program
Here, a web application host is created through the BuildWebHost
method, and the host initialization is completed through CreateDefaultBuilder
.
CreateDefaultBuilder(string[] args)
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
if (env.IsDevelopment())
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
}
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.UseIISIntegration()
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
});
return builder;
}
Some of the code seems to have been seen in ASP. Net core 1.0+
- .UseKestrel(): using KetrelServer as the HTTP server of Web application is a server with cross platform characteristics
- .UseContentRoot(Directory.GetCurrentDirectory()): use the configuration application root as the web server content root
- .ConfigureAppConfiguration(...): read the configuration file appsettings.json and add user sensitive data, environment variables and command-line parameters
- .ConfigureLogging(...): read the logging node in the configuration file and configure the log system
- .UseIISIntegration(): IIS express is used as the reverse proxy server of HTTP server, which is used for debugging during user development. When it is published in the official environment, you can choose IIS, Apache, nginx, etc
- .UseDefaultServiceProvider(...): sets the default dependency injection container.
3. Configuration of hosting service and request pipeline
Return to WebHostBuilder
and return to Program.cs
. Call the Startup
class through UseStartup
to further define the request processing pipeline and the services required for configuration of the application.
CreateDefaultBuilder(string[] args)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
- ConfigureServices:configure the services used in the application and add them to the dependency injection container. For example, asp.net MVC core framework, Entity Framework core, identity, etc
- Configure: configure HTTP request pipeline, similar to HttpHandler and HttpModule in asp.net. It is generally used to configure middleware
4. Create a host
After that, create the host through build, and then run
- Link : https://www.zdyla.com/en/post/aspnet-core-project-parsing.html
- Copyright Notice : Unless otherwise stated, please contact the author for authorization and indicate the source!