If you’re like me and constantly try to learn something new, you surely realized by now that the best way to learn a new, complicated topic is by explaining it to someone else.
For the last couple of weeks I’ve been reading up and trying my hand in C# and ASP.NET MVC. It’s going ok but there are still some things that feel a bit shaky in my mind. This is why I’m writing this post. This is a beginner’s understanding of ASP.NET MVC.
What is ASP.NET MVC?
What is .NET Framework and ASP.NET?
In short .NET Framework is a collection of technologies designed to help developers build a variety of applications such as rich Windows applications, command line tools or web applications. ASP.NET is one part of .NET Framework used to build web applications. It is an engine that hosts said web applications and a high-level development platform designed as a server-side technology, which means that all code executes on the server.
What is MVC?
MVC is a design (aka. architecture) pattern which can be used to design applications with any programming language. It is based on a principle of separation of concerns. This means that the business logic of the application is separate from the input logic (or interaction) and the presentation logic (or the UI).
Before MVC was introduced as the leading design pattern for ASP.NET applications, it used Web Forms. While Web Forms are still supported MVC is the future of ASP.NET-based applications. The two main advantages of MVC over Web Forms are:
- Web Forms use View State and postbacks to create the effect of statefulness. While this make some coding faster (and easier to learn) it also means that every time a page is requested all input field states are send to the server and back. This can result in slow applications (even if they are rather small).
- Thanks to the way it is built, MVC gives the developer more control over the code (including the final html markup) and the application’s behavior.
The abbreviation MVC stands for Model, View and Controller.
M is for Model
Model, or domain model, is the heart of the application. Domain is the real-world entities, operations and rules that exist in the industry, company or activity that the application will support. The domain model is the software representation of the domain. In practice that means that the model part of the application contains domain objects (classes) and methods that store and operate on data.
C is for Controllers
Controllers have .cs extension and their job is to receive and handle incoming http requests, manipulate data in the model and choose the view in which this data should be rendered to the user. Public methods in controllers are called action methods and these methods can be invoked from the browser through an URL. In MVC URLs do not correspond to physical files. Each action method has its own URL. This means that when user opens /, /Home, or /Home/Index, the Index() action method from HomeController.cs is called.
Controllers also return views by calling the View() method (return View(); – returns default view) which results in creating ViewResult object. This object instructs the application which view should be used to render the data.
V is for View
Views are files with the .cshtml extension. They are used to render html. Views are associated with action methods by naming conventions. Index action method will by default point to index view in the folder named after the controller file in which Index action method is placed.