Web Programming
Web Programming
CHAPTER 7
ASP.NET MVC5Overview
Web Programming
Web Programming
DESIGN PATTERNS
Design patterns represent solutions to problems that arise when developing
software within a particular context.
Patterns facilitate reuse of successful software architectures and designs and
help document systems.
Patterns explicitly capture expert knowledge and design tradeoffs and make
it more widely available
Patterns help improve developer communication
Pattern names form a common vocabulary
Web Programming
MVC PATTERN
Web Programming
What is MVC?
•Model–view–controller (MVC) is a software architecture pattern
•Code reusability and separation of concerns
Web Programming
Model
Set of classes that describes the data we are working with as well as the business
Rules for how the data can be changed and manipulated. May contain data validation rules
Often encapsulate data stored in a database as well as code used to manipulate the data
Most likely a Data Access Layer of some kind
Apart from giving the data objects, it doesn’t have significance in the framework
Web Programming
View
Defines how the application’s user interface (UI) will be displayed
May support master views (layouts) and sub-views (partial views or controls)
Web: Template to dynamically generate HTML
Web Programming
Controller
The core MVC component
Process the requests with the help of views and models
A set of classes that handles communication from the user
Overall application flow
Application-specific logic
Every controller has one or more "Actions"
Web Programming
MVC Frameworks
CakePHP(PHP)
CodeIgniter (PHP)
Spring (Java)
Perl: Catalyst, Dancer
Python: Django, Flask, Grok
Ruby: Ruby on Rails, Camping, Nitro, Sinatra
JavaScript: AngularJS, JavaScriptMVC, Spine
ASP.NET MVC (.NET Framework)
Web Programming
Programming, sc
ripting,
and markup lang
uages
https://survey.stackoverflow.co/2022/
Web Programming
Web framework
s and technologi
es
https://survey.stackoverflow.co/2022/
Web Programming
ASP.NET MVC
Web Programming
.NET Core Vs .NET Framework Vs .ASP.NET
https://www.aceinfoway.com/blog/
dotnet-maze-comparison
Web Programming
.NET Framework
A platform for creating and running Windows apps.
The .NET framework consists of developer tools, programming languages,
and libraries used to create desktop and web applications.
Games, web services, and websites can also be built using the .NET
framework.
The framework has been designed such that it can be used with any of these
common languages: c#, c++, Visual Basic, JScript, Cobol, etc.
Also, more than 60 programming languages are supported by the .NET
Framework, 11 of which were created and developed by Microsoft.
Web Programming
ASP.NET
ASP.NET is the Microsoft platform for developing Web Applications.
Using ASP.NET you can create e-commerce sites, data driven portals and just
about anything else you can find on the internet.
You don't need to paste together a jumble of HTML and JavaScript code.
Instead you can create full scale web apps by leveraging your knowledge of C#
coding and a design tool like Visual Studio.
Web Programming
ASP.NET MVC
MVC is a design pattern used to decouple
user-interface (view),
data (model),
application logic (controller).
This pattern helps to achieve separation of concerns.
Using the MVC pattern for websites,
• Requests are routed to a Controller that is responsible for working with the
Model to perform actions and/or retrieve data.
• The Controller chooses the View to display and provides it with the Model.
• The View renders the final page, based on the data in the Model.
Web Programming
Models & data
Create clean model classes and easily bind them
to your database.
Declaratively define validation rules, using C#
attributes, which are applied on the client and
server.
ASP.NET supports many database engines including
SQLite, SQL Server, MySQL, PostgreSQL, DB2 and more, as
well as non-relational stores such as MongoDB, Redis, and
Azure Cosmos DB.
Web Programming
Models & data
Simply route requests to controller actions,
implemented as normal C# methods.
Data from the request path, query string, and
request body are automatically bound to
method parameters.
Web Programming
Views with Razor
The Razor syntax provides a simple, clean, and lightweight way to render HTML
content based on your view. Razor lets you render a page using C#, producing
fully HTML5 compliant web pages.
Web Programming
Separationof Concerns
•Each component has one responsibility
•SRP–Single Responsibility Principle
•DRY–Don’t Repeat Yourself
•More easily testable
•TDD–Test-driven development
•Helps with concurrent development
•Performing tasks concurrently
•One developer works on views
•Another works on controllers
Web Programming
Extensible
•Replace any component of the system
•Interface-based architecture
•Almost anything can be replaced or extended
•Model binders (request data to CLR objects)
• The Common Language Runtime (CLR) is programming that manages the execution of
programs written in any of several supported languages, allowing them to share common
object-oriented classes written in any of the languages. It is a part of Microsoft's . NET
Framework.
•Action/result filters (e.g. OnActionExecuting)
•Custom action result types
•View engine (Razor, WebForms, NHaml, Spark)
•View helpers (HTML, AJAX, URL, etc.)
•Custom data providers (ADO.NET), etc.
• ADO.NET is a set of classes that expose data access services
Web Programming
The Technologies
•Technologies that ASP.NET MVC uses
•C# (OOP, Unit Testing, async, etc.)
•HTML(5) and CSS
•JavaScript (jQuery, React, KendoUI, etc.)
•AJAX, Single-page apps
•Databases (MS SQLandothers)
•ORM (Entity Framework and LINQ)
• Object Relation Mapping (ORM) is used for data processing from a relational database to
modeling (logical objects or domain classes) in the programming that is more easily usable
by code.
•Web and HTTP
Web Programming
MVC5 FEATURES
•ASP.NET Identity
•Bootstrap templates
•Attribute Routing
•ASP.NET scaffolding
•Authentication filters
Web Programming
ASP.NET Identity
• Testability: Allows to write unit tests for user-related application code.
• Login providers: Rather than just focusing on username / password
authentication, ASP.NET Identity understands that users often are
authenticated through social providers (for example, Microsoft Account,
Facebook, or Twitter) and Windows Azure Active Directory
• NuGet distribution is installed in your applications as a NuGet package.
• NuGet : An essential tool for any modern development platform is a mechanism through which
developers can create, share, and consume useful code. Often such code is bundled into "packages"
that contain compiled code (as DLLs) along with other content needed in the projects that consume
these packages.
Web Programming
Bootstrap Templates
When you created a new MVC project and ran it, you got a white square on a
blue background
Web Programming
MVC Routing
• Routing is a process of mapping the browser request to the controller action and return
response back.
• Each MVC application has default routing for the default HomeController.
• We can set custom routing for newly created controller.
• The RouteConfig.cs file is used to set routing for the application.
Web Programming
MVC Routing (cont.)
In Global.asax in the Application_Start() there is
RouteConfig.RegisterRoutes(RouteTable.Routes);
RoutesConfig class is located in /App_Start/ in internet applications template by default
Web Programming
Attribute Routing
uses attributes to define routes.
Attribute routing gives you more control over the URIs in your web application.
For example, a socially enhanced e-commerce website could have the following routes:
•{productId:int}/{productTitle} Mapped to ProductsController.Show(int id)
•{username} Mapped to ProfilesController.Show(string username)
•{username}/catalogs/{catalogId:int}/{catalogTitle} Mapped to
CatalogsController.Show(string username, int catalogId)
Web Programming
ASP. NET Scaffolding (yapı iskelesi)
Scaffolding is the process of generating boilerplate code based on your model classes.
It includes support for building powerful custom scaffolders, complete with custom
dialogs and a comprehensive scaffolding API.
Web Programming
Authentication Filters
ASP.NET MVC filters are used to add extra logic at the different levels of MVC
Framework request processing.
• Authentication Filter runs before any other filter or action method.
• Authentication confirms if you are a valid or invalid user.
• These filters implement the IAuthenticationFilter interface and
ActionFilterAttribute base class.
Web Programming
FIRST MVC5 APPLICATION
31
Web Programming
Creating MVC App
You can create a new MVC application using either Visual Studio 2019/2022
or Visual Studio 2019/2022 Express for Web2019. The experience in both
IDEs is very similar.
Web Programming
Creating MVC App
In the Create New Project, Select the ASP.NET Web Application (.NET Framework)
Web Programming
Creating MVC App
Web Programming
Creating MVC App
Web Programming
The MVC app structure
When you create a new ASP.NET MVC application
with Visual Studio, it automatically adds several files
and directories to the Project.
ASP.NET MVC projects created with the Internet
application template have eight top-level directories
Web Programming
The MVC app structure
Web Programming
The MVC app structure
Web Programming
The MVC app structure
The /Controllers directory, you’ll find that Visual Studio
added Controller class HomeController.cs by default
Web Programming
The MVC app structure
The /Views directory, you’ll find some
subdirectories-/Home, and /Shared - as well as several
template files with in them, were also added to the project
by default.
Web Programming
The MVC app structure
The /Content and /Scripts directories, you’ll find the CSS files
that is used to style all HTML on the site, as well as JavaScript
libraries that can enable jQuery* support within the application
*: jQuery is a fast, small, and feature-rich JavaScript library.