Anatomy of a MVC Request Response
Diagram for MVC Request Response |
To be short and precise:
- When a user sends a HTTP request through a browser, the controller which is the traffic hub receives the request and interacts with model to perform the operation.
- Model perform the business operation to complete the task and respond back to controller.
- Then the controller selects a View and passes requested data to the view to generate a page.
- The View engine reads view template and dynamically generates HTML that is sent to the user.
ActionResult Types in MVC
Following table list ActionResult Types in MVC:
Type | HelperMethod | Description |
---|---|---|
ContentResult | Content() | Raw text data |
EmptyResult | -- | Returnsnothing |
FileResult | File() | Base class to send file |
HttpUnauthorizedResult | -- | Returns HTTP 401 |
JavaScriptResult | JavaScript() | Returns and executes script |
JsonResult | Json() | JavaScript Object Notation |
PartialViewResult | PartialView | HTML snippet |
RedirectResult | Redirect() | Redirect to URL |
RedirectToRouteResult | RedirectToRoute() RedirectToAction() | Redirect to MVC route or action |
ViewResult | View() | Full HTML page |
The Tenets of MVC
Following are the tenets of MVC:
- Separation of concerns
Foundation concept of a good application is that it should be modularized. So that each module focuses on its own module. In MVC, Model knows nothing about the View and the Controller. The View knows nothing about the Controller and so on. The more you keep the application separate the more it will perform better and it will be more maintainable. That's separation of concerns.
- Convention over configuration
The ASP.NET MVC eliminates the need for extensive configuration settings by following various conventions. Example would be in MVC project has separate folders for Module, Views and Controller. Convention locations to locate code files that implement those different components. By keeping those component in convention folders you don't have to make extensive configuration changes.
- Keep it DRY: Don't Repeat Yourself
DRY is one of the design principle that MVC embraces. The point here is you should reduce the duplication of code and logic which help in making the application faster as well as easier to maintaining. For example you can encapsulate chunk of reusable user interface code in partial view temples which are similar to asp.net user controls.
- Be helpful, but get out of the way
MVC allows you to have more control over HTML then ASP.NET web pages. It help you to fully customize the HTML view as per your need.
Subscribe to:
Posts (Atom)