Web Applications are programs that are executed on a web server and accessed from a web browser.
Three Layers of a web application:-
- Presentation Layer
- Business Logic Layer
- Data Layer
Consists of the interface through which the users interact with the application.
Business Logic Layer:-
Consists of the components of the application that control the flow of execution and communication between the presentation layer and the data layer.
Consists of the components that expose the application data stored in databases to the business logic layer.
Architecture of a web Application:-
- Single Tier
- Two Tier
- Three Tier
In single tier architecture, all the three layers are integrated together and can be installed on a single computer. If the application needs to be accessed on multiple computers a separate installation required.
In two tier architecture, the three layers are distributed over two tiers, a client and a server. The presentation layer resides on each client computer, the business logic layer resides either on client or on server, and data access layer resides on the server.
Types of two tier architecture:-
1> Fat Client and thin server:-
The architecture in which the business logic layer resides on the client is known as the fat client and thin server.
2> Fat server and thin client:-
The architecture in which the business logic layer resides on the server is known as the fat server and thin client.
Three Tier architecture:-
In three tier architecture, the three layers of the application are placed as three different entities. This architecture is used for those applications in which merging the business logic layer with the presentation layer or the data layer may degrade the performance of the application.
MVC( Model View Controller):-
Refers to a set of classes that describes the data that the applications work with.
Refers to the components that define an application user’s interface
Refers to a set of classes that handle communication from the user and the overall application flow. A controller responds to user input , communicates with the model, and decides the view to render.
Features of ASP.NET MVC:-
MVC has a powerful routing system. Routing is a feature that enables you to develop applications with comprehensible and searchable URLs.
MVC provides a scaffolding feature that provides a quick way to generate the code for commonly used operations in a standardized way.
Convention over Configuration:-
Asp.Net MVC Supports Convention over Configuration, which is a software design paradigm that aims at reducing the number of decisions taken by developers.
Bundling and Minification:-
Structure of an ASP.NET MVC Projects:-
Working With Controllers and Views
What is Controller?
Controllers within an MVC Application are responsible for processing incoming user requests, executing the appropriate application code, communicate with the model, and rendering the required view. In other words controller manage the flow of application. Controllers are implemented in an MVC application as C# classes inherited from System.Web.Mvc.Controller class. A controller class contains an application logic in the form of various public methods called action methods.
Creating a Controller:-
- Open Visual Studio and select Asp.Net Web Application. A popup window show you the list of templates where you can select “Empty”, “Web Forms”, “MVC”, “Web API” etc..
- Select MVC and click on OK. Then it will take some time to load your project.
- After loading the project go to solution explorer and you have the options like below screen shot.
- To create a controller goto the Controllers folder given in the solution explorer.
- Right click on the controller and goto add then select controller.
- A new window will appear where you have three options if you use Visual Studio 2015.
- You need to choose MVC5 Controller-Empty.
- Then click on Add.
- Give the name of your controller then click on Add button. It will add controller in Controllers folder.
Create Action Methods:-
An Application needs to handle user interactions, such as entering a url in the web browser, clicking a hyperlink and submitting a web form. Each of these interaction sends a request to the web server. In ASP.NET MVC such requests are handled by public methods in the controllers. These public methods are called action methods. You can define these action methods in the controller class.
How to Pass parameters in controller actions:-
Working with Views:-
A view is a combination of HTML markup and code that runs on the web server. The code is embedded in the web pages using razor syntax. A web application contains multiple views, where each view is responsible for either displaying some information to users or taking input from users.
To add view right click on Action Method Name and select Add view option.
Passing Data from a controller to a View:-
Two techniques to pass data from a controller to a view:-
ViewData is a dictionary of objects derived from the System.Web.Mvc.ViewDataDictionary class. It enables you to pass the data between a controller and a view. Here, values can be set using key/values pairs in the controller action method.
ViewBag is a dynamic object that allows passing data between a controller and a view. You can add properties of multiple types and their values by using the ViewBag object in a controller action method.
On Controller Action Method:-
Difference between ViewData and ViewBag and TempData:-
- ViewData is used to pass data from controller to view
- It is derived from ViewDataDictionary class
- It is available for the current request only
- Requires typecasting for complex data type and checks for null values to avoid error
- If redirection occurs, then its value becomes null
- ViewBag is also used to pass data from the controller to the respective view
- ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
- It is also available for the current request only
- If redirection occurs, then its value becomes null
- Doesn’t require typecasting for complex data type
- TempData is derived from TempDataDictionary class
- TempData is used to pass data from the current request to the next request
- It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
- It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages
Razor is a markup syntax that allows you to embed server side code (written in c# or VB) in an HTML markup. Razor is a clean, lightweight and simple view engine with a minimum amount of syntax and code. Razor allows embedding server side code in a view.
Razor Syntax Rules:-
- The @ character indicates a transition from markup to code.
- Razor code statements end with semicolon(;).
- The C# code is case sensitive.
- Variables should be declared by using the var keyword.
- Strings should be enclosed within quotation mark.
A partial view is a part of the view that can be used on multiple pages. Partial views are crated in the views/shared folder. The name of the partial view is prefixed with an (_) character. You can render a partial view in a view using @Html.Partial() method.
Steps to create a partial view:-
- Goto views folder and click on it.
- Right click on the shared folder inside the views folder.
- Click on Add then choose view.
- Enter the name of the partial view with the prefixed underscore character. Then click on add. It will create a partial view.
- To render a partial view on a view use method @Html.Partial(“name of your partial view here”);