What is routing in ASP.NET Core?
Routing is the mechanism that matches an incoming HTTP request to an application endpoint and can also generate URLs for endpoints.
ASP.NET Core interview questions covering routing, controllers & model binding.
Open a question for the full answer, code and interview guidance.
Routing is the mechanism that matches an incoming HTTP request to an application endpoint and can also generate URLs for endpoints.
Conventional routing defines route patterns centrally, usually in Program.cs. The pattern is applied across controllers and actions according to naming conventions.
Attribute routing defines routes directly on controllers and actions using attributes such as Route, HttpGet, HttpPost, HttpPut, and HttpDelete.
Conventional routing defines general route templates in one central location and relies on controller/action conventions. Attribute routing places route templates directly on controllers and actions.
Route parameters are variable segments in a route template. Their values are extracted from the URL and made available to model binding.
Route constraints restrict which URL values can match a route parameter. Built-in constraints include int, guid, bool, datetime, length, min, max, and others.
An optional route parameter is marked with a question mark, such as {id?}. The route can match whether or not that segment is present.
A controller is a class that participates in handling HTTP requests in the MVC pattern. It receives input, coordinates application services, and returns an action result.
ControllerBase provides the core functionality needed for HTTP endpoints, including model state, status-code helpers, request information, and action result helpers. It is commonly used for APIs.
An action method is a public controller method that can be selected by routing to handle an HTTP request.
IActionResult is an abstraction representing the result of an MVC action. Different implementations can produce views, redirects, JSON responses, files, status codes, or other HTTP responses.
Common MVC results include ViewResult for rendering a view, RedirectResult and RedirectToActionResult for redirects, JsonResult for JSON, ContentResult for text, FileResult for files, and status-code results such as NotFoundResult or BadRequestResult.
ActionResult<T> allows an API action to return either a strongly typed value of T or an ActionResult such as NotFound(), BadRequest(), or Unauthorized().
Model binding converts data from an HTTP request into .NET action parameters or model properties.
Binding source attributes explicitly tell ASP.NET Core where a value should come from.
For complex types, model binding creates an object and binds matching request values to its public settable properties or supported constructor parameters.
ModelState stores information about model binding and model validation for the current request. It contains attempted values and any binding or validation errors.
Model validation runs after model binding and evaluates validation rules associated with the bound model.
ApiController enables API-focused controller conventions and behaviors. It is normally applied to API controllers, often together with attribute routing.
Overposting occurs when a client submits fields that should not be editable and model binding assigns those values to properties on the bound object.
A ViewModel is shaped for a UI view and may combine display data, input fields, selections, or presentation-specific properties.
Attributes such as HttpGet, HttpPost, HttpPut, HttpPatch, and HttpDelete restrict an action to specific HTTP methods and can also define route templates.
Anti-forgery protection helps defend browser-based applications against Cross-Site Request Forgery (CSRF), where a malicious site causes a user's browser to submit an unwanted request using the user's existing authentication context.
A redirect tells the client to make a new request to another URL. MVC provides helpers such as Redirect(), RedirectToAction(), RedirectToRoute(), and their permanent variants.
Design routes to be stable, readable, and unambiguous. Use route constraints where they improve matching, and choose conventional or attribute routing according to the application's URL requirements.