Namespace Microsoft.AspNetCore.Mvc
Contains types that support the Model-View-Controller (MVC) pattern for building web applications using ASP.NET Core. This namespace includes classes for controllers, actions, filters, model binding, view features, and more.
Namespaces
- ActionConstraints
- ActionFilters
- ApplicationModels
- Binding
- Controllers
- Core
- Filters
- Formatters
- Infrastructure
- ModelBinding
- ModelBinding.Binders
- ModelBinding.Metadata
- Razor
- RazorPages
- Routing
- ViewFeatures
Classes
public abstract class Controller
Base class for all controllers.
public class ControllerBase
A base class for controllers that do not have view support.
public class ControllerContext
Context information for a controller.
public class ApiControllerAttribute : Attribute
Marks a controller class as an API controller.
public class RouteAttribute : Attribute
Specifies the route template for a controller or action.
Interfaces
public interface IActionResult
Represents an action result in an MVC application.
public interface IActionFilter : Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata
Interface for action filters.
public interface IAsyncActionFilter : Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata
Interface for asynchronous action filters.
Enums
public enum ControllerState
Represents the state of a controller.
Controllers
Types related to controller management and implementation.
public class ControllerActionDescriptor
Describes an action method in a controller.
public class ControllerModel
Describes a controller.
Action Constraints
Types for constraining action execution.
public class NonActionAttribute : Attribute
Marks a method as not being an action method.
Filters
Types for implementing cross-cutting concerns in MVC.
public class ActionFilterAttribute : Attribute, Microsoft.AspNetCore.Mvc.Filters.IActionFilter, Microsoft.AspNetCore.Mvc.Filters.IOrderedFilter
Base class for action filters.
public class AuthorizeAttribute : Attribute, Microsoft.AspNetCore.Mvc.Filters.IFilterFactory, Microsoft.AspNetCore.Mvc.Filters.IOrderedFilter
Specifies that the requested resource requires authorization.
View Features
Types related to view rendering and data binding.
public class ViewResult : Microsoft.AspNetCore.Mvc.ActionResult
Represents an IActionResult that performs a redirect.
public class ViewDataAttribute : Attribute
Attribute to specify a custom ViewDataDictionary.
public class ViewDataDictionary
A dictionary that can be used to pass data between a controller and a view.
Commonly Used Methods and Properties
From ControllerBase
| Member | Type | Description |
|---|---|---|
| Request | HttpRequest | Gets the HttpRequest. |
| Response | HttpResponse | Gets the HttpResponse. |
| ModelState | ModelStateDictionary | Gets the ModelStateDictionary. |
| Ok() | IActionResult | Returns an OkResult. |
| Ok(object value) | IActionResult | Returns an OkObjectResult. |
| NotFound() | IActionResult | Returns a NotFoundResult. |
| NotFound(object value) | IActionResult | Returns a NotFoundObjectResult. |
| BadRequest() | IActionResult | Returns a BadRequestResult. |
| BadRequest(object error) | IActionResult | Returns a BadRequestObjectResult. |
Example Usage
using Microsoft.AspNetCore.Mvc;
namespace MyWebApp.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IActionResult GetAllProducts()
{
// In a real app, you'd fetch data from a service
var products = new[] {
new { Id = 1, Name = "Laptop", Price = 1200.00m },
new { Id = 2, Name = "Keyboard", Price = 75.00m }
};
return Ok(products);
}
[HttpGet("{id}")]
public IActionResult GetProductById(int id)
{
// Simulate finding a product
if (id == 1)
{
var product = new { Id = 1, Name = "Laptop", Price = 1200.00m };
return Ok(product);
}
return NotFound($"Product with ID {id} not found.");
}
[HttpPost]
public IActionResult CreateProduct([FromBody] Product newProduct)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// Simulate saving the product
newProduct.Id = 3; // Assign a new ID
return CreatedAtAction(nameof(GetProductById), new { id = newProduct.Id }, newProduct);
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}