CSC378 ยท 2079 ยท 5 marks
Define HTTP. Explain the MVC pattern.
Define HTTP. Explain the MVC pattern. [5]
Quick answer
HTTP stands for HyperText Transfer Protocol. It is the foundation protocol of data communication on the World Wide Web. HTTP defines how messages are formatted and transmitted between a client (such as a web browser) and a server, and what actions web servers and browsers should take in response to various commands.
HTTP and MVC Pattern
Part 1: HTTP (HyperText Transfer Protocol)
Key characteristics of HTTP:
- It is a request-response protocol: the client sends an HTTP request and the server returns an HTTP response.
- It is stateless: each request is independent and the server does not retain session information between requests.
- It operates over TCP/IP and commonly uses port 80 (HTTP) or port 443 (HTTPS).
- Common HTTP methods include: GET, POST, PUT, DELETE, PATCH.
ASP.NET Web API is "a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices", confirming HTTP as the core communication protocol for web-based services.
Part 2: MVC Pattern
MVC stands for Model, View, and Controller. It is an architectural/design pattern that separates an application into three main components, making the application easier to develop, test, and maintain.
Components of MVC
1. Model
- Represents the shape of the data.
- A class in C# is used to describe a model.
- Model objects store data retrieved from the database.
- In short: Model represents the data.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
2. View
- A View in MVC is the user interface (UI).
- It displays model data to the user and also enables them to modify it.
- In ASP.NET MVC, View is written using HTML, CSS, and special Razor syntax that makes it easy to communicate with the Model and Controller.
3. Controller
- The Controller handles the user request.
- Typically, the user interacts with the View, which raises an HTTP request.
- The Controller acts as the request handler -- it processes the request, manipulates the Model, and returns the appropriate response/View.
MVC Flow Diagram
User
|
v
[View] ---Send Request---> [Controller] ---Manipulate---> [Model]
^ | |
| | |
+--------Renders/Display<------+----------------------------+
Step-by-step flow:
- The user sends a request through the View (browser).
- The Controller receives and processes the HTTP request.
- The Controller interacts with the Model to retrieve or update data.
- The updated Model data is passed back to the View.
- The View renders and displays the result to the user.
Advantages of MVC Pattern
| Feature | Benefit |
|---|---|
| Separation of Concerns | Each component has a distinct responsibility |
| Testability | Components can be tested independently |
| Maintainability | Easy to modify one component without affecting others |
| Reusability | Models and Views can be reused |
As stated in the notes: "ASP.NET MVC gives us a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives us full control over markup for enjoyable, agile development."
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.