Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Building host environment to start the application (Core 1.

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.

Configuration Sources in ASP.NET Core:


1. appsettings.json
2. appsettings.{Environment}.json
3. User Secrets.
4. Environment variables.
5. Command line arguments

o Note: 5 overrides 4, 4 overrides 3, 3 overrides 2, 2 overrides 1


To Access configuration information:
o IConfiguration Service.

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();

Note: 1- UseDefaultFiles must be declared before UseStaticFiles


2- default files are [ index.htm, index.html, default.htm, default.html ]

 To change the default page:

 We can combine UseDefaultFiles & UseStatic Files() & UseDirectoryBroswer


By FileServerOptions

Development Environments:
1) Development: code with exception page with all development tools.
2) Staging: similar to production with user friendly error.
3) Production

Start Up Class methods:


1) ConfigureServices: is the dependency injection container.
2) Configure: is the Application requests processing pipeline (application middlewares).
 To start MVC project in empty project:
 in ConfigureServices -> services.AddMvc();
 in Configure -> app.UseMvcWithDefaultRoute();
 Configure execution order:
1. Developer Exception Method.
2. app.UseDefaultFiles()
3. app.UseStaticFiles()
4. app.UseMvcWithDefaultRoute()
5. app.Run()

Difference between AddMvc() & AddMvcCore():


 AddMvcCore() method only adds the core MVC services.
 AddMvc() method adds all the required MVC Services.
 AddMvc() method calls AddMvcCore() method internally
AddMvc() = AddMvc() + AddMvcCore()

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.

2) ViewBag (Loosely typed view):


a. ViewBag.key = value;
b. In cshtml, we don’t need to explicity cast anything, just ViewBag.key, EX:
ViewBag.Title; , ViewBag.Employee.Name;
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.

3) Strongly typed view:


a. return View(model);
b. In cshtml:
i. In top of HTML: @model slnName.Models.Employee
ii. In HTML: @Model.Name, @Model.Email, @Model.Age
c. Provides compile-time checking.
4) ViewModel:
a. If you want to customize the objects sent to View, by creating new Class, include
these objects in it, then pass an object of this class to return View(…);

Sections in Layout Page:


 Used to provide a way where certain part of page should be placed.
 ‫يعنى لو عاوز اضيف جزء معين فى صفحه ما الى الصفحه الما‬
 Syntax:
o In _Layout: @RenderSection(“Section Name”,required:true/false)
o In view: @section SectionName { <script bla bla bla> }
 This part will be placed in _Layout where @RenderSection is placed.

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.

 Attribute Routing: Specifing the route in the Controller class itself.

You might also like