Professional Documents
Culture Documents
Dot Net Core
Dot Net Core
Dot Net Core
NET CORE
ASP.NET CORE MVC
ASP.NET CORE IDENTITY --For Authentication
ENTITY FRAMEWORK ---For Data Access
ASP.NET CORE:
- Cross Platform, High Performance, Open Source framework to build Cloud based, Internet connected apps.
- Redesign of ASP.Net 4.X. Was initially called .NET 5, Later called as .NET CORE.
- One programming model for .NET CORE MVC & WEB API. -------------xxx
- Built In Support for Dependency Injection. ------------------------xxx
The answer to this question depends on the project requirement, what our project demands for so here are some points that we have to consider to choose best for our
project out of the .NET Framework and .NET Core.
Both, .NET 3.1, and .NET Core 2.1 will have long term support.
.NET Core 2.x applications run on .NET Core as well as .NET Framework.
Build ASP.NET CORE:
EMPTY- Bare min.content to display Hello World. Setup from scratch manually.
API - TO create ASP.NET CORE Restful Http service. It doesn’t have a UI so it doesnt create UI Folders such as Views, css, js etc. It only has Controllers Folder.
Web Application - Uses New razor pages framework. Slimmer version of MVC
Razor Class Library - to create a reusable razor class lib project. Contains all reusable UI pieces like data models, page models. Can be re used in multiple apps
Angular - .net with angular
React.js -
React .js and Redux -
.csproj:
Note:
ASP.NET Core project files and folders are synchronized with physical files and folders. If you add a new file or folder in project folder then it will directly reflect in
the solution explorer. You don't need to add it in the project explicitly by right clicking on the project.
Main calls CreateWebHostBuider method
CreateWebHostBuider calls CreateDefaultBuilder(args) static method of WebHost Class.
CreateDefaultBuilder method sets up the web host that hosts our appl with preconfigured defaults.& configures logging.
(B) OutOfProcess
In .csproj file <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
OR completely remove <AspNetCoreHostingModel></AspNetCoreHostingModel> from .csproj, By default it’s OutOfProcess hosting.
appsettings.json:
Usually stored as Key value pairs
Example:
Added MyKey in appsettings.json
Inject Iconfiguartion in startup.cs -> Using Microsoft.Extensions.Configurations and then ctor+ tab (twice) for constructor, print from startup.cs
Appsettings.{Environment}.json:
Appsettings.Development.json
Appsettings.Staging.json
Appsettings.Production.json
** Settings in Appsettings.{Environment}.json will override settings in Appsettings..json
Middleware:
Its a very broad term.
A piece of software that can handle HTTP Request or HTTP Response.
Middleware components that we use to setup a request pipeline. It is this pipeline that determines how a request is processed. The request pipelone is configured as part
of application startup by Configure() Method in Startup Class.
As we know that prog execution starts from Main() which calls CreateWebHostBUilder() which calls CreateDefaultBuilder() method which sets up default WebHost.
As part of setting up the web host, it also sets up the Starup by .UseStartup<Startup> Extension method.
Has access to both incoming req and outgoing response.
Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component:
Chooses whether to pass the request to the next component in the pipeline.
Can perform work before and after the next component in the pipeline.
Each middleware component in the request pipeline is responsible for invoking the next component in the pipeline or short-
circuiting the pipeline. When a middleware short-circuits, it's called a terminal middleware because it prevents further middleware
from processing the request.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-6.0
*****https://www.tutorialsteacher.com/core/aspnet-core-middleware
We can configure middleware in the Configure method of the Startup class
using IApplicationBuilder instance. The following example adds a single middleware using Run method
which returns a string "Hello World!" on each request.
});
Method Signature:
public static void Run(this IApplicationBuilder app, RequestDelegate handler)
Method Signature:
public delegate Task RequestDelegate(HttpContext context);
Example: Use()
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Hello World From 1st Middleware!");
await next();
});
The above example will display Hello World From 1st Middleware!Hello World From 2nd Middleware! in the
browser.
Middleware Description
Authentication Adds authentication support.
CORS Configures Cross-Origin Resource Sharing.
Routing Adds routing capabilities for MVC or web form
Session Adds support for user session.
StaticFiles Adds support for serving static files and directory browsing.
Diagnostics Adds support for reporting and handling exceptions and errors.
Open project.json and add Microsoft.AspNetCore.Diagnostics dependency if it is not added. Wait for some
time till Visual Studio restores the packages.
This package includes following middleware and extension methods for it.
We can call respective Use* extension methods to use the above middleware in the configure method of
Startup class.
Let's add welcomePage middleware which will display welcome page for the root path.
The above example will display the following welcome page for each request.
This way we can use different Use* extension methods to include different middleware.
Configure:
Method that sets up a request processing pipeline for ASP.NET CORE application.
By deafult contains 2 Middleware. => UseDeveloperExceptionPage(), Run(async (context) )
By default, a default page can have a name from one of the foll:
Default.htm
Default.html
Index.htm
Index.html
DeveloperException Page:
The Developer Exception Page displays detailed information about unhandled request exceptions. ASP.NET Core apps enable the
developer exception page by default when running in the Development environment.
The developer exception page runs early in the middleware pipeline, so that it can catch unhandled exceptions thrown in
middleware that follows.
If the Exception Page is run later in the middleware pipeline, it cannot display the exception page.
Detailed exception information shouldn't be displayed publicly when the app runs in the Production environment.
The Developer Exception Page can include the following information about the exception and the request:
Stack trace
Query string parameters, if any
Cookies, if any
Headers
Configuring Using Environment Variables:
ASPNETCORE_ENVIRONMENT Variable sets the runtime env.
Use IHostingEnvironment service to access the runtime env.
Can be set from:
-Launchsettings.json
-Control Panel
-Right click- properties - Debug- set
If env variable is present in both launchsettingd & control panel, then the value value from control panel will be overridden to the
value from launchsettings.json
Model: Set of classes that repr data and the logic to manage the data
Employee class has data which we want to display, EmpRepository class knows how to Save and manage the data from db
Here, we have used interface IEmployeeRepository to make use of Dependency Injection so that we can create loosely coupled/
easily testable systems.
Setup MVC
Step 1: add req mvc services to the built in dependency injection container
//services.AddMvc() into the ConfigureServices() method in Starup class
Step 2: Add MVC middleware
Add Mvc middleware in Configure()
//app.UseMvcWithDefaultRoute() or app.UseMvc()
AddMvc() vs AddMvcCore():
Controller:
Views:
By default, when executed the control goes to the View depending on the name of Action method.
If the action method is: public ViewResult Details() in Home Controller., then it looks for ‘Details.cshmtl’ View in ‘Home’ folder in ‘Views’ (Views/Home/Details) By Default
TO change the default view: mention the name of view to be returned while returning as shown below
If the req view is in another folder, mention the exact path:
Extension .cshtml is not required while using a relative path but req. while mentioning the absolute path as above