The default templates generate the schema given below.
The new folder in ASP.NET Core is wwwroot and it stores all the static files in our project. The static files means that the HTML files, CSS files, image files, and JavaScript files which are sent to the users' browsers should be stored inside the wwwroot folder.
We can add or remove project dependencies.
The entry point of an ASP.NET Core 1.0.
- using System.IO;
- using Microsoft.AspNetCore.Hosting;
-
- namespace HelloWorldDotnetCore
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- var host = new WebHostBuilder()
- .UseKestrel()
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseIISIntegration()
- .UseStartup<Startup>()
- .Build();
-
- host.Run();
- }
- }
- }
WebHostBuilder()
The builder pattern of ASP.NET Core and creates a web application host.
UseKestrel()
We are all familiar with the Web Browser Internet Information Service (IIS) in ASP.NET. In cross-platform, the Web Server for ASP.NET Core is Kestrel and it is supported on all the platforms and versions that .NET Core supports.
UseContentRoot()
UseContentRoot is required for specifying the root content directory for hosting in IIS and IIS Express.
Example :
ContentRoot : D:\HelloWorldDotnetCore(Our App Name)\
UseWebRoot()
It contains the Web-servable content files. In ASP.NET Core Web.Config is besides the WebRoot.
Example
WebRoot: D:\HelloWorldDotnetCore(Our App Name)\wwwroot\
UseIISIntegration()
Why are we using two web servers in a code? Since it uses UseKestrel and UseIISIntegration, which are different in ASP.NET Core, they both are created with different actions.
UseIISIntegration IIS is only used as a reverse proxy in ASP.NET Core. UseKestrel creates the Web Server and hosts the code.
UseStartup<Startup>()
The Startup class is helpful to define the request handling pipeline and configure the services required by the app. We can declare our startup class name inside the “<>”.
Build & Run
Both methods build the IWebHost, which will host the app and start it wait for incoming HTTP requests.
project.json
In this project.json, file contains the version information and details of ASP.NET Core framework, tools, build, run and publish options, scripts and dependencies of all the files, like front-end etc. It clearly mentions that it is the replacement of Web.Config & Pacage.Config file.
- {
- "dependencies": {
- "Microsoft.NETCore.App": {
- "version": "1.0.1",
- "type": "platform"
- },
- "Microsoft.AspNetCore.Diagnostics": "1.0.0",
- "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
- "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
- "Microsoft.Extensions.Logging.Console": "1.0.0",
- "Microsoft.AspNetCore.Mvc": "1.0.1"
- },
-
- "tools": {
- "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
- },
-
- "frameworks": {
- "netcoreapp1.0": {
- "imports": [
- "dotnet5.6",
- "portable-net45+win8"
- ]
- }
- },
-
- "buildOptions": {
- "emitEntryPoint": true,
- "preserveCompilationContext": true
- },
-
- "runtimeOptions": {
- "configProperties": {
- "System.GC.Server": true
- }
- },
-
- "publishOptions": {
- "include": [
- "wwwroot",
- "web.config"
- ]
- },
-
- "scripts": {
- "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
- }
- }
Startup.cs
The startup class is helpful to define the request handling pipeline and configure the Services required by the app. The code given below is the one where we added services.AddMvc() method. With the help of NuGet Package Manager, we can install all the dependencies files for MVC.
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
-
- namespace HelloWorldDotnetCore
- {
- public class Startup
- {
- public Startup(IHostingEnvironment env)
- {
-
- }
-
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc();
- }
-
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- loggerFactory.AddConsole();
-
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.Run(async (context) =>
- {
- await context.Response.WriteAsync(" Welcome to Dotnet Core !!");
- });
-
-
- }
- }
- }
IApplicationBuilder
In Middleware, the request delegates are used to build the request pipeline or it handles each HTTP request. IApplicationBuilder provides all the important extension methods (Use, run and map ) for configuring request delegates.
IHostingEnvironment
It provides the information about the Web hosting environment.
ILoggerFactory
It provides the log factory information or records the log information from each middleware request pipline in our project.
appsettings.json
This file is not available in an empty template. Thus, we can add through “Ctrl + Shift + A ” or “Add -> New Item – Type JSON in search area -> Select ASP.NET Configuration File “