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