ASP.NET Core MVC is a powerful framework for building web applications using the Model-View-Controller design pattern. This cheat sheet provides a quick reference for common concepts and syntax.
Controllers handle incoming requests, interact with models, and select views to render.
| Return Type | Description | Example |
|---|---|---|
IActionResult |
Base interface for action results. | public IActionResult Index() |
ViewResult |
Renders a view. | return View("MyView", model); |
PartialViewResult |
Renders a partial view. | return PartialView("_MyPartial", model); |
RedirectResult |
Performs an HTTP redirect. | return Redirect("/products"); |
RedirectToActionResult |
Redirects to another action. | return RedirectToAction("Index", "Home"); |
NotFoundResult |
Returns a 404 Not Found status. | return NotFound(); |
OkResult |
Returns a 200 OK status. | return Ok(); |
StatusCodeResult |
Returns a specified HTTP status code. | return StatusCode(401); |
JsonResult |
Returns JSON data. | return Json(new { msg = "Success" }); |
Views display data to the user and contain Razor syntax for dynamic content.
@: Starts a Razor expression.@{ ... }: C# code block.@Model: Accesses the strongly-typed model passed to the view.@ViewBag: Dynamic property bag for passing data.@ViewData: Dictionary for passing data.@Html.DisplayNameFor(model => model.PropertyName): Renders display name.@Url.Action(...): Generates URL for an action.@await Html.PartialAsync("_MyPartial") or @await Html.RenderPartialAsync("_MyPartial").Tag helpers enable server-side C# code to participate in HTML element parsing and rendering.
Models represent the data and business logic of the application.
Used to enforce data integrity.
[Required][StringLength(maxLength)][Range(minValue, maxValue)][EmailAddress][Phone][RegularExpression("pattern")]Defines how URLs are mapped to controller actions.
Applied directly to controllers and actions.
Reusable UI components that can be called from views or controllers.
Tag Helpers allow you to write HTML with C# logic, providing a more familiar syntax for front-end developers.
| Tag Helper | Purpose | Example |
|---|---|---|
<form asp-controller="..." asp-action="..."> |
Renders an HTML form with action and controller attributes. | <form asp-controller="User" asp-action="Login"> |
<a asp-controller="..." asp-action="..."> |
Renders an anchor tag with URL routing. | <a asp-controller="Product" asp-action="Details" asp-route-id="123">View Product</a> |
<input asp-for="..."> |
Generates an input element for a model property, including validation attributes. | <input asp-for="Email" class="form-control" /> |
<label asp-for="..."> |
Generates a label for a model property. | <label asp-for="Password"></label> |
<div asp-validation-for="..."> |
Renders a validation message for a model property. | <div asp-validation-for="Name" class="text-danger"></div> |
<environment include="..." exclude="..."> |
Conditionally renders content based on the environment (Development, Staging, Production). | <environment include="Development">...</environment> |
Filters allow you to inject pre- and post-processing logic into the MVC execution pipeline.
IActionFilter: Executes before and after the action method.IAuthorizationFilter: Executes before the action method to perform authorization.IResourceFilter: Executes at the beginning and end of the resource pipeline.IExceptionFilter: Executes when an unhandled exception occurs.IResultFilter: Executes before and after the action result is executed.