Introduction
ASP.NET MVC is a framework used to build web applications using the Model-View-Controller pattern. It helps in separating the application logic, user interface, and data, which makes the code easy to manage, test, and scale. This guide is a simple reference for key topics in ASP.NET MVC, including definitions, short examples, and tips. It is useful for both beginners and those who need a quick refresh.
1. Model-View-Controller (MVC) Pattern
2. Routing
- Definition: Routing maps URLs to controller actions.
- Example
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
- Important: Defined in
RouteConfig.cs
, used by Global.asax
.
3. Controller
4. ActionResult Types
5. Views
6. Strongly Typed Views
7. HTML Helpers
8. Form Submission
9. Model Binding
10. Model Validation
11. TempData, ViewBag, ViewData
12. Filters
13. Partial Views
14. Layout View
15. Bundling and Minification
16. Error Handling
17. Scaffolding
- Definition: Automatically generates a controller and views for a model.
- Example: Right-click Model → Add → Controller → Use Scaffolding.
- Important: Saves time during prototyping.
18. Entity Framework Integration
19. Dependency Injection (DI)
20. Security
- Authentication: Use Identity for user login and registration.
- Authorization: Use
[Authorize]
to restrict access.
- Anti-Forgery
@Html.AntiForgeryToken()
- Important: Always validate and sanitize inputs.\
21. Areas
- Definition: Areas are used to split a large application into smaller sections or modules.
- Example
// In AreaRegistration
public class AdminAreaRegistration : AreaRegistration {
public override string AreaName => "Admin";
public override void RegisterArea(AreaRegistrationContext context) {
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
- Important: Helps organize projects with many controllers and views.
22. Custom Route Constraints
23. Custom HTML Helpers
24. JsonResult
25. File Upload
- Definition: Upload files to server using a form.
- Example
<form enctype="multipart/form-data" method="post">
<input type="file" name="file" />
<input type="submit" />
</form>
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file) {
if (file != null) {
var path = Path.Combine(Server.MapPath("~/Uploads"), file.FileName);
file.SaveAs(path);
}
return View();
}
- Important: Set
enctype="multipart/form-data"
in the form.
26. Session Management
27. Using ViewModels
28. AJAX with jQuery
29. Web.config Settings
30. Anti-Forgery Token
31. Authorization and Authentication
32. Custom Error Pages
33. Output Caching
34. Action Filters
Conclusion
ASP.NET MVC is a strong and flexible framework for building web applications. Knowing the key concepts like routing, controllers, views, model binding, and validation helps in creating clean and maintainable applications. This cheatsheet gives a quick look at the important topics, with examples and points to remember. It is a useful reference when you are coding or revising.