Presentation+ASPNET+MVC+Core+ +Part+I
Presentation+ASPNET+MVC+Core+ +Part+I
Liviu-Adrian Cotfas,
liviu.cotfas@ase.ro
PhD.
ASP.NET MVC Core, C# Language
C#
• url: https://docs.microsoft.com/en-us/aspnet/
2
About me
https://ro.linkedin.com/in/cotfasliviu
3
Administrative issues
4
Administrative issues
5
Administrative issues
6
Administrative issues
API reference:
https://docs.microsoft.com/en-us/dotnet/api/?view=aspnetcore-2.
2
Source code:
https://github.com/aspnet/Home
Various samples:
https://code.msdn.microsoft.com/
7
Contents
8
Brief History of the Microsoft Web
Stack
Brief History of the Microsoft Web Stack
ASP.NET MVC
Launched in 2007 (CTP), 2009 (1.0);
12
Brief History of the Microsoft Web Stack
ASP.NET MVC
Although solving the problem of the slow release cycle and
removing the HTML markup abstraction, ASP.NET MVC still suffered
from:
One couldn't run .NET applications on a non-Windows system.
The dependency on the full framework made the .NET apps less
suitable for high-density scenarios, like the cloud, where
hundreds of applications run on a single machine and must scale
up very fast.
13
Brief History of the Microsoft Web Stack
ASP.NET Core
built on .NET Core, which is a cross-platform version of the .NET
Framework without the Windows-specific application programming
interfaces (APIs).
14
Brief History of the Microsoft Web Stack
ASP.NET Core
provides the functionality of the original ASP.NET MVC Framework
and includes the functionality that was previously provided by Web
API.
15
HelloWorld (ASP .NET Core 2)
First application
During setup make sure that you select the .NET Core Cross-
Platform Development workload.
17
First application
18
First application
New Project
19
First application
20
First application
21
Model-View-Controller Pattern
MVC Pattern
MVP MVC
The model which contain or represent the data that users work
with.
24
MVC Pattern
The logic that manipulates the data in the model is contained only
in the model;
The code that handles user requests and input is contained only in
the controller.
Models are the definition of the universe your application works in.
26
Models
The model is also responsible for preserving the overall state and
consistency of the data—for example, making sure that all
transactions are added to the ledger and that a client doesn’t
withdraw more money than he is entitled to or more money than
the bank has.
27
Models
Tip Many developers new to the MVC pattern get confused with
the idea of including logic in the data model, believing that the
goal of the MVC pattern is to separate data from logic. This is a
misapprehension: the goal of the MVC pattern is to divide an
application into three functional areas, each of which may contain
both logic and data. The goal isn’t to eliminate logic from the
model. Rather, it is to ensure that the model only contains
logic for creating and managing the model data.
29
Controllers
Views should:
Contain the logic and markup required to present data to the
user
32
ASP.NET Core MVC
34
ASP.NET Core MVC
35
Key Benefits of ASP.NET Core
Key Benefits of ASP.NET Core
MVC Architecture
37
Key Benefits of ASP.NET Core
Extensibility
ASP.NET Core is built as a series of independent components that
have well-defined characteristics, satisfy a .NET interface or that
are built on an abstract base class. Thus, it is possible to replace
key components with ones of your own implementation.
In general, the ASP.NET Core MVC gives you these three options for
each component:
Use the default implementation of the component as it stands
(which should be enough for most applications).
Derive a subclass of the default implementation to tweak its
behavior.
Replace the component entirely with a new implementation of
the interface or abstract base class.
38
Key Benefits of ASP.NET Core
Testability
The ASP.NET Core MVC architecture gives you a great start in
making your application maintainable and testable because you
naturally separate different application concerns into independent
pieces. In addition, each piece of the ASP.NET Core platform and
the ASP.NET Core MVC framework can be isolated and replaced for
unit testing, which can be performed using any popular open
source testing framework, such as xUnit.
/App_v2/User/Page.aspx?action=show
%20prop&prop_id=82742
/to-rent/chicago/2303-silver-street
41
Key Benefits of ASP.NET Core
This gives you control over your URL schema and its relationship
to your application, offering you the freedom to create a pattern of
URLs that is meaningful and useful to your users, without the need
to conform to a predefined pattern.
43
Key Benefits of ASP.NET Core
Modern API
Microsoft’s .NET platform has evolved with each major release,
supporting—and even defining—the state-of-theart aspects of
modern programming.
ASP.NET Core MVC is built for .NET Core, so its API can take full
advantage of language and runtime innovations familiar to C#
programmers, including the await keyword, extension methods,
lambda expressions, anonymous and dynamic types, and
Language Integrated Query (LINQ).
Many of the ASP.NET Core MVC API methods and coding patterns
follow a cleaner, more expressive composition than was possible
with earlier platforms.
44
Key Benefits of ASP.NET Core
Cross-Platform
Previous versions of ASP.NET were specific to Windows, requiring a
Windows desktop to write web applications and a Windows server
to deploy and run them. Microsoft made ASP.NET Core cross-
platform, both for development and for deployment. .NET Core is
available for different platforms—including Linux and OS X/macOS
—and is likely to be ported to others.
45
Key Benefits of ASP.NET Core
Open Source
Unlike previous Microsoft web development platforms, you are free
to download the source code for ASP. NET Core and ASP.NET Core
MVC and even modify and compile your own version of it.
You can download the ASP.NET Core and ASP.NET Core MVC source
code from https://github.com/aspnet .
46
Project Structure
Visual Studio Project Templates
48
Visual Studio Project Templates
49
Visual Studio Project Templates
The templates are just different starting points into the same
functionality, and you can add whatever functionality you need to
projects created with any of the templates.
So, the real difference between the project templates is the initial
set of libraries, configuration files, code, and content that Visual
Studio adds when it creates the project.
/Areas Areas are a way of partitioning a large application into smaller pieces.
/Dependencies The Dependencies item provides details of all the packages a project
relies on.
/Components This is where view component classes, which are used to display
selfcontained features such as shopping carts, are defined.
/Controllers This is where you put your controller classes. This is a convention. You
can put your controller classes anywhere you like, because they are all
compiled into the same assembly.
52
Visual Studio Project Structure
/Data This is where database context classes should be defined, but they are
also frequently defined in the Models folder.
/Migrations This is where details of database schemas are stored so that
deployment
databases can be updated.
/Models This is where you put your view model and domain model classes. This
is a convention. You can define your model classes anywhere in the
project or in a separate project.
53
Visual Studio Project Structure
/Views This directory holds views and partial views, usually grouped together
in folders named after the controller with which they are associated.
/Views/Shared This directory holds layouts and views that are not specific to a single
controller.
54
Visual Studio Project Structure
55
Visual Studio Project Structure
56
Convention Over Configuration
57
Convention Over Configuration
You just follow a certain naming convention for your files, and
everything just works. There is less flexibility in changing your
project structure when dealing with this kind of convention.
58
Convention Over Configuration
Principles
avoid configuring things that can be inferred;
all convention-based defaults can be overridden.
Example:
No need to explicitly configure associations between controllers and their
views;
59
Convention Over Configuration
60
Controller Conventions
61
View Conventions
The default view for an action should be named after that method.
ex: for an action Index, the view should be named Index.cshtml
62
Layout Conventions
63
A More Complex Project
Demo
https://github.com/liviucotfas/ase-web-a
nd-cloud-applications-security/
65
Thank you!