7_lecture 3 - Getting Started With ASP Net Core
7_lecture 3 - Getting Started With ASP Net Core
NET CORE
Learning Objectives
1.Since ASP.Net Core 2.0, Visual Studio utilizes .csproj for project management.
2.Double-clicking on .csproj enables property settings adjustment.
3.Allows changes to target framework and other implicit settings in the project SDK.
FOLDER STRUCTURE
launchSettings.json
• launchSettings.json:
• Includes visible setting profiles and debug settings.
• Profiles cover details like iOS settings, application port number, and environment (development/production).
• Essential configuration for launching and debugging ASP.NET Core applications.
• Ensures flexibility in managing settings based on profiles and deployment environments.
wwwroot
• wwwroot Folder:
• wwwroot Folder.
• Houses static files (CSS, JavaScript, Bootstrap,
libraries, images) for use in applications.
• Serves as a centralized location for managing
and accessing static resources.
Pages
• Pages Folder:
• Contains sample pages.
• Default Pages:
• Automatically run when the application is
executed.
• Referred to as reserve pages.
• Provide a starting point for understanding and
exploring ASP.NET Core application structure.
DEMO:
BUILDING A
WEB
APPLICATION
13
14
15
MODEL-VIEW-CONTROLLER
COMMUNICATION •Request Handling:
•User request is received by the controller.
•Controller determines whether to communicate with the
view for rendering or with the model for data preparation.
•Controller Actions:
•If rendering is required, the controller interacts with the
view using return view in the action method.
•If data preparation is needed, the controller
communicates with the model or database, prepares the
data, and then sends it to the view.
•Interaction Dynamics:
•View and controller do not directly interact.
•Controller acts as a middle iterator or binder between the
view and the model.
•User Response:
•Controller responds back to the browser, providing the
final output expected by the user.
MODEL-VIEW-CONTROLLER
COMMUNICATION
• Model to Model Communication:
• This is achieved via parameters/composition.
• Model to View Communication:
• This follows a specific path: Model > Controller > View.
• Direct movement from model to view isn’t possible.
• The process involves creating a model object in the controller before passing
it on to the view.
• The steps for passing data or communicating from model to view are:
o Taking the object in the action of a controller.
o Passing the model object as a parameter to view.
o Using @model to include the model on the view page.
MODEL-VIEW-CONTROLLER
COMMUNICATION
• Model to Controller Communication:
• This involves creating an object of the Model class in Controller to access the
Model in Controller.
• View to Model Communication:
• This follows a specific path: View > Controller > Model, indicating that direct
movement from View to Model is not possible.
• Data must be submitted first to the controller before passing it on to the
model.
• The steps for this communication are:
o Submitting an HTML form to a Controller.
o Creating an object of Model in Controller.
o Passing values to the Model object.
MODEL-VIEW-CONTROLLER
COMMUNICATION
• View to View Communication:
• We can move from one view to another view by using partial views.
• View to Controller Communication:
• We can move data from view to controller by submitting forms to controllers
or by:
o JSON
o AJAX Calls
o JavaScript
o Partial Views
MODEL-VIEW-CONTROLLER
COMMUNICATION
• Controller to Model Communication:
• We can move from Controller to Model just like we move from Model to
Controller.
• View to Controller Communication:
• We can move data from controller to view in the following ways:
o By using viewBag
o ViewData
o TempData
• Controller to Controller Communication:
• We can move from one Controller to another by using RedirectToAction(), and
then pass the name of the specific action.
DEMO:
HANDLING
REQUESTS IN
.NET MVC CORE
22
23
ACTION
METHODS AND
RESULT TYPES
ACTION METHODS
Request goes to the particular Action Result and the type of data
that action method result returns that View is being rendered based
upon the result set.
ACTION RESULT
Result Class Description Base Controller Method
• The Razor View Engine is the default View Engine for the ASP.NET
Core apps.
• It looks for Razor markup in the View File and parses it and produces
the HTML response.
• It is used to generate views that are returned to the client’s browser
in response to a request.
• It is a powerful and flexible templating engine.
• With this, it is easier to create dynamic web pages in ASP.NET Core.
• It enables developers to write clean and maintainable code that can
be easily understood.
RAZOR VIEW ENGINE
• RazorView engine
integrates HTML markup
tags with C# code.
• C# code is sent to the
View Engine.
• View Engine parses the
request into pure HTML.
• The resulting HTML is
then displayed in the
browser.
RAZOR VIEW ENGINE
38
DEMO: RAZOR VIEW ENGINE
39
DEMO: RAZOR VIEW ENGINE
40
DEMO: RAZOR VIEW ENGINE
41
Customer.cs
DEMO: RAZOR VIEW ENGINE
43
DEMO: RAZOR VIEW ENGINE
44
LAYOUT,
SECTIONS AND
VIEWSTART
LAYOUT VIEW
• HTML HELPER:
• EQUIVALENT HTML
DEMO: TAG HELPER
ROUTING
ATTRIBUTE ROUTING
• Used to transfer temporary data not included in the model from the
controller to the view.
• It is a dynamic type property of the ControllerBase class, which is the
base class of the Controller class.
• It is often used for simple scenarios where a strongly typed model is
not required.
DEMO: VIEWBAG
DEMO: VIEW BAG
VIEWDATA
• Used to pass data from the controller action method to a view. This
data can be displayed on the view.
• Works on the principle of Key-value pairs.
• This type of binding is known as loosely binding.
• Can pass any type of data like normal integer, string, and even
objects.
• ViewData only transfers data from controller to view, not vice-versa. It
is valid only during the current request.
DEMO: VIEWDATA
ASP.NET CORE
FORMS
ASP.NET CORE FORM
• Key part of building web applications using the ASP.NET Core framework.
• Allow users to submit data to a server.
• Can be used to create, update, or delete records, or to perform some other
action.
• Can be created using HTML helpers, which are methods that generate
HTML code to display and interact with forms.
• When a user submits a form, the data is typically sent to the server using a
POST request.
• The server then receives the data and can use it to perform the desired
action, such as updating a database record.
ASP.NET FORM
WEAKLY-TYPED FORMS STRONGLY-TYPED FORM
• Technique where data is not explicitly bound to a specific data model.
• Explicit Data Binding:
• Form data is accessed as a collection of key-value pairs.
• Data tied explicitly to a specific data model.
• Keys represent the names of form fields and values represent user input.
• Developers can access this using the Request object. • Data Model Definition:
• Allows developers to read and manipulate form data directly without needing to create a specific data model. • Developers create a data model mirroring form data structure.
• Model Binding Process:
• Automates mapping form data to data model.
• Structured Data Models:
• Classes/structures define form field properties and data types.
• Automatic Mapping:
• Form data mapped automatically to data model properties.
• Compile-Time Validation:
• Occurs during compile-time for early error detection.
• Reduced Runtime Errors:
• Model binding minimizes the risk of runtime errors.
DEMO: WEAKLY-TYPED FORM
DEMO: WEAKLY-TYPED FORM
DEMO: WEAKLY-TYPED FORM
DEMO: WEAKLY-TYPED FORM
DEMO: STRONGLY-TYPED FORM
DEMO: STRONGLY-TYPED FORM
DEMO: STRONGLY-TYPED FORM
DEMO: STRONGLY-TYPED FORM
MODEL BINDING
MODEL BINDING
• For example, let’s say you have an action method that takes a Person
object as a parameter
• When the client submits a form that includes data for a Person object
DEMO: MODEL BINDING
DEMO: MODEL BINDING
DEMO: HANDLING
FORMS POST
Q&A