Mid MVC

Explain the MVC pattern.?

MVC stands for Model–View–Controller, a design pattern that separates application logic

into three layers:

Follow :

  • Model – Represents the data and business logic (e.g., Product, Customer,

Order).

  • View – The UI or presentation layer that displays data (Razor views).
  • Controller – Handles user requests, interacts with the model, and returns responses

(views or data).

Example:

A user requests /Products/Details/1 →

ProductsController.Details(1) → fetches product → returns the

Details.cshtml view.

This separation makes the app more maintainable and testable.

⚙ 2. How are controllers activated in ASP.NET Core?

Controllers are created by the ControllerActivator, which uses Dependency Injection (DI)

to resolve dependencies.

You don’t manually instantiate controllers — the framework does it for you.

Example:

public class ProductsController : Controller

private readonly IProductService _service;

public ProductsController(IProductService service) => _service =

service;

When a request matches the route /products, the DI container automatically provides

IProductService.

Follow :

🧱 3. What is the role of the ControllerBase and

Controller classes?

  • ControllerBase: Base class for API controllers — includes core MVC features like

routing, model binding, and IActionResult.

  • Controller: Inherits from ControllerBase and adds View support (e.g., View(),

ViewBag, etc.).

Example:

// For APIs

public class ProductApiController : ControllerBase { }

// For MVC Views

public class ProductController : Controller { }

🔗 4. What is the difference between Controller and

ApiController?

Feature Controller ApiController

Purpose Returns views (HTML) Returns data (JSON/XML)

Inheritanc

Controller ControllerBase with

[ApiController]

Features ViewBag, View(), PartialView() Automatic model validation, attribute routing

Example return View(product); return Ok(product);

⚡ 5. What are Action Methods?

Action methods are public methods in a controller that handle HTTP requests.

Follow :

Example:

public class ProductController : Controller

public IActionResult Details(int id)

var product = _service.GetById(id);

return View(product);

Notes:

  • Action methods cannot be private or static.
  • They respond to routes like /Product/Details/5.

🎯 6. What are Action Results?

An Action Result represents the response returned to the client — view, JSON, redirect,

file, etc.

ASP.NET Core provides many result types:

  • ViewResult
  • JsonResult
  • RedirectResult
  • FileResult
  • ContentResult
  • StatusCodeResult

Follow :

🔍 7. Difference between ViewResult, JsonResult, and

ContentResult.

Type Used For Example

ViewResult Returns an HTML view return View("Details",

model);

JsonResult Returns JSON data return Json(model);

ContentResul

Returns plain text or custom

content

return Content("Hello

World");

🔹 8. What is IActionResult?

IActionResult is an interface that represents the result of an action method.

It allows flexibility — you can return any kind of result (view, JSON, redirect, etc.) from the

same method.

Example:

public IActionResult Index()

if (!User.Identity.IsAuthenticated)

return RedirectToAction("Login");

return View();

🧠 9. How does model binding work?

Model binding automatically maps data from the HTTP request (query string, route, body,

form) to method parameters or model objects.

Example:

Follow :

public IActionResult Save(Product model)

// model properties are automatically filled

_service.Add(model);

return RedirectToAction("Index");

If the request body contains { "Name": "Laptop", "Price": 1200 },

ASP.NET Core binds it to the Product object.

✅ 10. How does model validation work?

Model validation checks whether the incoming model meets data annotation rules before

executing the action.

Example:

public class Product

[Required]

public string Name { get; set; }

[Range(1, 10000)]

public decimal Price { get; set; }

In a controller:

if (!ModelState.IsValid)

return View(model);

If [ApiController] is used, invalid models automatically return 400 Bad Request.

🧩 11. What are filters in ASP.NET Core MVC?

Follow :

Filters are components that allow code to run before or after specific stages of request

processing in MVC — e.g., authorization, action execution, results, etc.

They provide cross-cutting concerns like logging, caching, or exception handling.

🧱 12. Explain different types of filters.

Filter Type Purpose Runs When

Authorization Filter Checks if user is authorized Before everything

Resource Filter Run before/after model binding Around action

Action Filter Run before/after action

methods

Around controller actions

Exception Filter Handle unhandled exceptions When an exception

occurs

Result Filter Run before/after action results Around result execution

🔢 13. What is filter ordering?

Filters run in a specific order:

More from ASP.NET Core MVC Tutorial

All questions for this course