Building Host Environment To Start The Application (Core 1.0)
Building Host Environment To Start The Application (Core 1.0)
Building Host Environment To Start The Application (Core 1.0)
0)
WebHostBuilder : Is a class to create and configure a host for a web application.
UseKestrel() : A method to specify kestrel as the internal web server.
UseContentRoot : Specifies the current directory as a root directory which will be src
folder in the default ASP.NET Core project.
UseIISIntegration : Specifies the IIS as the external web server.
UseStartup : Specifies the startup class.
Build : Returns an instance of IWebHost using the configuration specified
above.
host.Run() : Start the web application
Notes:
Startup.cs == Global.asax
AspNetCoreHostingModel:
inProcess -> ASP.Net core uses only one web server to host the application (the iisexpress)
iisexpress for development purpose only
iis for production
outOfProcess -> it has two web servers – Internal & external Web server
o Internal is Kestrel
o External can be IIS, Nginx or Apache depending on the OS.
o Kestrel: Cross-platform Web Server for ASP.Net Core, can be used by itself, the process
used to host the app is dotnet.exe
From a performance standpoint, inProcess is better than outOfProcess
In the project setting *.csproj, if there is no
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
Then its default is OutOfProcess.
Static Files:
All application static files are included in wwwroot.
To enable default files like index.html, in Startup.cs Configure method: app.UseDefaultFiles();
To enable static files, in Startup.cs Configure method: app.UseStaticFiles();
Development Environments:
1) Development: code with exception page with all development tools.
2) Staging: similar to production with user friendly error.
3) Production
MVC
Steps:
1) Creating Model: class that represents prop of a table. [Employee.cs]
2) Creating Interface Model: interface that represents the signature logic of this model.
[IEmployeeRepository.cs]
3) Creating Mock Model Class: class that implements the interface
[MockEmployeeRepository.cs]
4) Dependency Injection Configuration for this model in Startup.cs ConfigurationServices()
services.AddSingleTon<IEmployeeRepository, MockEmployeeRepository>();
5) Creating Controller:
Handles the incoming http Request.
Builds the model.
Returns the model data to the caller if we are building API.
Select a view and pass the model data to view if we are building MVC.
The view generates the required HTML to present the data.
Notes:
1) The purpose of creating model interface, is the dependency injection.
2) Dependency Injection major lifetimes: [SingleTon, Scoped, Transient].
Passing Data to View from Controller:
1) ViewData (Loosely typed view):
a. ViewData[“key”] = value;
b. In cshtml, ViewData type is string, if we store any other value type, we should
explicity cast it, EX: var emp = ViewData[“Employee”] as
slnName.Models.Employee;
c. Loosely means: the view doesn’t know the type of data we’re storing at compile
time, the view will know the type of data that we have stored in these keys at
runtime.
ViewStart:
Adds _Layout to each view Page automatically, rather than adding _Layout to each view
Page manually.
Sub Folder _ViewStart overwrites the parent Folder.
Layout defined in the view Page, overwrites sub & Parent Folders.
ViewImports:
Used to include the common namespaces
، Sln Name.Model.BlaBla زى الـ، كلهNameSpace بكتب اسما الـView بدل ماكل مره فى اى صفحه
ViewImports بكتب كل دا فى صفحه
EX: @model IEnumerable<Employee.Models.Employee>
o Change to @model IEnmerable<Employee>
o In _ViewImports.cshtml: @using Employee.Models;
Sub Folder _ViewImports overwrites the parent Folder.
ViewImports defined in the view Page, overwrites sub & Parent Folders.
Routing:
Conventional Routing: Basic routing Configuration in startup.cs.