ASP.net Core 6 Fundamentals
ASP.net Core 6 Fundamentals
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Chapter 1 - .NET Universe
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
What is .NET Core?
• 3 different technologies:
– .NET Framework (Windows only)
– .NET Core (Cross platform)
– .NET Standard (For both .NET Framework + .NET Core)
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
ASP.NET vs ASP.NET Core
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
.NET 6 Application Patterns
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Chapter 2 – Introduction to WebApp / Web API
• Web Application Framework
• MVC (Model-View-Controller)
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
What .NET Core can do
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Web Application Framework
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Client Rendered UI
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Server Rendered UI
– A web UI app that renders on the server dynamically generates the page's HTML and CSS on the
server in response to a browser request.
– The page arrives at the client ready to display.
– Netflix, Pinterest, Paypal, etc.
Advantages
– The client requirements are minimal because the server does the work of logic and page generation.
– Flexibility of access to protected server resources
– Static site analysis advantages, such as search engine optimization.
Disadvantages
– As the full page has to reload every time, the overall page rendering becomes slow
– User interactions require a round trip to the server to generate UI updates
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
ASP.NET Core MVC
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
MVC (Model-View-Controller)
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
MVC (Model-View-Controller)
• Views - Part of the application that presents the data content through the user interface.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
MVC Features
• Easy and frictionless testability. Highly testable, extensible and pluggable framework
• To design a web application architecture using the MVC pattern, it offers full control over
your HTML as well as your URLs
• Clear separation of Business, Ul and Input logic using Model, View, and Controller
respectively.
• URL Routing for SEO (Search Engine Optimization) Friendly URLs. Powerful URL- mapping
for comprehensible and searchable URLs
• Support for Test Driven Development (TDD)
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Web API
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
API Types
• REST
– The Representational State Transfer (REST or RESTful) is the best-known API standard. REST defines
routes with a URL
• SOAP
– The Simple Object Access Protocol (SOAP) is another major API protocol. A SOAP API can
communicate over other major internet communication protocols, such as TCP and SMTP, in addition
to HTTP.
• RPC
– The Remote Procedure Call (RPC) protocol can return XML or JSON responses. It differs from SOAP
and REST APIs in a few key ways. As the name suggests, this protocol calls a method rather than a
data resource.
• GraphQL
– GraphQL is a distinct query language, with best practices for its use. GraphQL uses HTTP, similar to a
REST API, transmitting text data in the payload of each request.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
REST API
• Client-server architecture:
– The API interface remains on the client and is separated from the data kept on the server.
• Statelessness:
– Each request made to the API is independent of all others.
• Cacheable:
– A REST API response may be able to retrieve cached data, but you need to specify whether or not your
responses can be cached.
• Layered:
– The API works the same whether it interacts directly with the server or if there are other layers, like a
load balancer or proxy service, between the client and the server.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
API Methods and HTTP Verbs
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
HTTP Status Code
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
API Tool
Postman
– Postman is an API platform for building and using APIs.
– It simplifies each step of the API lifecycle and streamlines collaboration.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
API Tool
Swagger
– Swagger is a set of tools for implementing the OpenAPI specification
– OpenAPI is a specification for describing RESTful APIs
– Specifies an API's endpoints, expected payloads and sample responses
– Aimed to help developers communicate with an API
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Swagger overview of Endpoint Group
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Swagger Endpoint Details
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Chapter 3 – ASP.NET Core features
• Routing
• Dependency injection
• Serialization
• Environment & variables (secrets.json, appsettings.json, launch.json)
• Middleware
• Golden Path
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Routing
Routing in ASP.NET Core application is a mechanism to match incoming HTTP requests and dispatch
those requests to the app's executable endpoints, such as controllers, methods, etc.
– When a user request URLs from the server then URLs are handled by the routing system.
– The Routing system try to find out the matching route pattern of requested URL with already registered
routes which are map to controller, actions, files, or other items.
– If there is a matching route entry, then it processes the request, otherwise it returns 404 error.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Routing types
• Convention-based Routing
– Route is determined based on the conventions defined in the route templates which will map the
incoming Requests (i.e. URLs) to controllers and their action methods.
– In ASP.NET Core MVC application, the Convention based Routes are defined within the Configure
method of the Startup.cs class file.
– MapControllerRoute( name: “home", pattern: "{controller}/{action}/{id}");
• Attribute-based Routing
– Route is determined based on the attributes which are configured either at the controller level or at the
action method level.
– Route can be controlled and passed to controller and action by using the attributes that decorate
controllers and methods.
– Flexible, more explicit, reduce routing errors, simple to add more controllers and actions.
• Mixed Routing
– Mix the use of conventional routing and attribute routing. It's typical to use conventional routes for
controllers serving HTML pages for browsers, and attribute routing for controllers serving REST APIs.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Attribute-based Routing Example
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Controller (Method)
[HttpGet]
public IEnumerable<WeatherForecast> GetForecast()
{
return Enumerable.Range(1, 5)
.Select(index => _weatherRepository.GetForecast(DateTime.Now.AddDays(index)))
.ToArray();
}
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Model
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Endpoints
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Inversion of control (loC)
• Inversion of control (IoC) is a software design principle that states a class should be configured by another class from the
outside, as opposed to configuring dependencies statically.
• It can benefit in pluggability, testability, usability and loose coupling if the management of an application's flow is
transferred to a different part of the application.
• Dependency injection describes a specific use case of IoC -- one where a container provides a configurable application
context that creates, starts, catches and manages pluggable objects.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Dependency Injection
• A design pattern for achieving Inversion of Control (IoC) between classes and their dependencies.
• Used to make a class independent of its dependencies or to create a loosely coupled program.
• Key part of building loosely coupled applications, improving the reusability of code.
• By decoupling the usage of an object, more dependencies can be replaced without needing to change
class.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Dependency Injection
• Advantages
– Dependency injection relieves various code modules from the task of instantiating references to
resources and enables dependencies.
– Programs are more testable, maintainable and reusable.
– Developers can create classes independently that still use each other. They only need to know the
interface of the classes.
– Dependency injection helps in unit testing directly as well.
• Disadvantages
– Can be hard to troubleshoot and debug.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Object Lifetime
• Singleton:
– IoC container will create and share a single instance of a service throughout the application's lifetime.
• Scoped:
– IoC container will create an instance of the specified service type once per request and will be shared
in a single request.
• Transient:
– IoC container will create a new instance of the specified service type every time you ask for it.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Service registration methods
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Dependency Injection Sample Code
builder.Services.AddScoped<IWeatherRepository, WeatherRepository>();
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Lab 1 – Controllers, Routing and Dependency Injection
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Serialization
Serialization is the process of converting the state of an object into a form that can be
persisted or transported. The complement of serialization is deserialization, which converts a
stream into an object. Together, these processes allow data to be stored and transferred.
– JSON serialization serializes only public properties and does not preserve type fidelity. JSON is an
open standard that is an attractive choice for sharing data across the web.
– Binary serialization preserves type fidelity, which means that the complete state of the object is
recorded and when you deserialize, an exact copy is created.
– XML and SOAP serialization serializes only public properties and fields and does not preserve type
fidelity. This is useful when you want to provide or consume data without restricting the application that
uses the data.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
JSON Serialization
• Common JSON:
{
“field": {
"id": "203194F2-BF3D-4BE9-92CD-9869C883ED07",
“criteria": {
"range": {
"min": 1,
"max": 100
}
}
}
}
• Serialized JSON
{"field":{"id":"203194F2-BF3D-4BE9-92CD-9869C883ED07","criteria":{"range":{"min":1,"max":100}}}}
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Serialization and deserialization example
string jsonString =
@"{
""Date"": ""2023-04-01T00:00:00-07:00"",
""TemperatureCelsius"": 75,
""Summary"": ""Fools' Day""
} ";
var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString);
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Serialization in ASP.NET
[Produces("application/json")]
[Consumes("application/json")]
public class SampleController : ControllerBase
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Model Validation
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Model Validation Sample
[Required]
[StringLength(8, ErrorMessage = "Description can't be longer than 8")]
public string Description { get; set; }
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Environments
• Development:
– A development environment in software and web development is a workspace for developers to make
changes without breaking anything in a live environment
• Staging:
– The last step before something goes into production and is visible on the live site
• Production:
– The latest versions of software, products, or updates are pushed live to the intended users
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Configuration in ASP.NET Core
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Configuration
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Configuration Sample appsettings.json
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Configuration Priorities
Use Case For common For environment- Local use only, stores Used in deployed Local use only, can
settings across all specific settings settings in an environments define multiple profiles
environments obfuscated location
but does not encrypt
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Lab 2 – Serialization and Model Validation
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Middleware
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.
– The ASP.NET Core request pipeline consists of a sequence of request delegates, called one after the
other.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Middleware Order
The following diagram shows the complete request processing pipeline for ASP.NET Core MVC and Razor
Pages apps.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Middleware Registration Sample Code
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Golden Path
• The Golden Path, or the Key User Journey as it is also called, is the key set of steps that a user takes to find a product’s
real value.
• This path should be the ideal default and not focus on exceptions or errors.
• Knowing your product’s ideal path will help you focus your work.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Chapter 4 - Testing and documentation
• Swagger
• Debugging
• Log
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Swagger Integration
• Swagger can be easily incorporated into an ASP.NET web API via the Swashbuckle library
• [ApiExplorerSettings(GroupName = "notes")]
Copyright © 2022 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Developer exception page
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Developer exception page
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Developer Exception Page Sample Code
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Debugging
• Add breakpoints to controller action under test, and make a request to execute the action
• Sample debugging session:
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Log
• ILoggerFactory and ILogger instances are by default injected into the service collection.
• Loggers log to several locations by default, including the console.
• Other logging sinks can be configured by registering more logging providers.
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Lab 3 – Middleware
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
Questions (?)
Contacts:
James Geng - [email protected];
Jeremy Ling - [email protected];
George Wilkins - [email protected]
Tamanna Rahman - [email protected]
Kevin Reitano - [email protected]
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
References
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.
The End
Copyright © 2023 FactSet Research Systems Inc. All rights reserved. Confidential: Do not forward.