C Sharp MVC

From Pigbert Wiki

  • In an ASP.NET MVC application, a URL corresponds to a controller action instead of a page on disk.

Controller

Useful Action Attributes:

  • [ActionName("Name")]: allows action method to have a different name than what it is supposed to match to.
  • [AcceptVerbs("GET|POST")]: same as below
  • [AcceptPost], [AcceptGet]: same as above
  • [PostOnly]: action can only be accessed using HTTP POST
  • [DefaultAction]: action that handles all the action requests that do not exist
  • [NonAction]: hide the unfinished action from public
  • [Layout("MyCustomMasterPage")]: can be used on the controller Class or action method or both. It specifies which master class to use for rendering.
  • [Rescue("Error"[,OnlyHandleExceptionType])]: redirect to Views/Shared/Rescues/Error.aspx in case of failure, or to Views/Shared/Rescues/ThrownExceptionType.aspx if the .aspx file exists and the type of causal exception matches.


Public controller actions exposed by the HomeController class always return a subclass of the base ActionResult class:

  • ViewResult (return View()) – Represents HTML and markup.
  • EmptyResult – Represents no result.
  • RedirectResult (return Redirect()) – Represents a redirection to a new URL.
  • RedirectToRouteResult (return RedirectToAction()|RedirectToRoute()) – Represents a redirection to a new controller action.
  • JsonResult (return Json()) – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  • ContentResult (return Content()) – Represents a text result. If a controller action returns a result that is not an action result – for example, a date or an integer – then the result is wrapped in a ContentResult automatically.
  • XmlResult (return Xml(object[])) - The rss type xml... (MvcContrib Extension)

View

A view contains the HTML markup and content that is sent to the browser. A view is the equivalent of a page when working with an ASP.NET MVC application.

You must create your views in the right location, corresponds to the class name and action name of the controller in which the view is created. The HomeController.Index() action returns a view located at the following path: \Views\Home\Index.aspx; The HomeController.About() action returns a view located at the following path: \Views\Home\About.aspx

In general, if you want to return a view for a controller action, then you need to create a subfolder in the Views folder with the same name as your controller. Within the subfolder, you must create an .aspx file with the same name as the controller action.


Model

An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic and database access logic. For example, if you are using LINQ to SQL to access your database, then you would create your LINQ to SQL classes (your dbml file) in the Models folder.

A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action. Everything else should be contained in the model.

In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.

Personal tools