MVC Introduction - ablealias/MVC GitHub Wiki

MVC – Model View Controller

MVC it is a useful way to organize code, markup and control flow logic.

Model – The class that deals with data and encapsulate application business logic.

View – The presentation and UI components

Controller – The classes that handle user interaction and manage control flow in an application.

User request are coming into your MVC application are handled by actions, which are methods of a Controller class. All action methods returning an ActionResult object, the ActionResult class is the base class for all action results. You can decide which type of action result to return based on the task that the action method is performing. Built-in action result types are listed below,

  • ViewResult – View: Renders a view as a Web page.
  • PartialViewResult – PartialView: Renders a partial view, which defines a section of a view that can be rendered inside another view.
  • RedirectResult – Redirect: Redirect to another action method by using its URL.
  • RedirectToRoute – RedirectToAction / RedirectToRoute: Redirect to another action method.
  • ContentResult – Content: Returns a user defined content type.
  • JsonResult – Json: Returns a serilized json object.
  • JavaScriptResult – JavaScript: Returns a script that can be executed on the client.
  • HttpStatusCodeResutl – None: Returns a specific http response code and description.
  • HttpUnauthorizedResult – None: Returns the result of an unauthorized HTTP request.
  • HttpNotFoundResult – HtttpNotFound: Indicates the requested resource was not found.
  • FileResult – File: Returns binary output to write to the response.
  • FileContentResult – Controller.File(Byte[],String): Sends the contents of a binary file to the response.
  • FilePathResult - Controller.File(String,String): Sends the contents of a file to the response.
  • FileStreamResult – Controller.File(Stream,String): Sends binary content to the response through a stream.
  • EmptyResult – (None): Represents a return value that is used if the action method must return a null result.

MVC Request Routing

Application route table is created in the application’s Global.asax file. The Global.asax file is a special file that contains event handlers for ASP.NET application life cycle events. The asp.net routing engine relies on URL patterns to decide which controller action will handle a given request.

When MVC application first starts, the Application_Start() method is called. This method in turn, calls the RegisterRoute() method. The RegisterRoute() method creates the route table. The Default route table contains a single route (named Default). The first segment of URL to a controller name, the second segment of a url to a controller action, and the third segment to a parameter name id. Here is an example routing table,

routes.MapRoute(
 name:"Serial",
 url:"serial/{letterCase}",
 defaults:new {controller="Home",action="Serial",letterCase="upper"}
 );

Action Selectors

Action Selectors are a type of attribute you can use to decorate your controller methods in order to help the routing engine determine which one to select to server request in cases where it might otherwise be clear. An attribute appears inside square brackets just above the method declaration.

[HttpPost]
public ActionResult Create(Customer customer)
{
	Db.Customers.Add(cusotmer);
	Db.SaveChanges();
	return RedirectToAction(β€œIndex”);
}

[HttpGet]
public ActionResult Create()
{
	return View();
}

Advantages of MVC

  • Separation of concerns (SoC) the organization of code within MVC is very clean, organized and granular, making it easier for a web application to scale in terms of functionality.
  • Enables the full control over the rendered HTML.
  • Enables Test Driven Development (TDD).
  • Easy integration with JavaScript frameworks.
  • Following the design of stateless nature of the web.
  • No ViewState and PostBack events
  • Meaningful URLs or SEO friendly URLs