Sunday 2 August 2015

ASP.Net MVC Controllers


  • Controllers process incoming HTTP requests from users. 
  • Each HTTP request is handled by a specific controller. 
  • ASP.NET MVC implements the concept of controllers with .NET classes that have methods to process such requests.
  • The methods in a controller are called action methods because they return an object of type ActionResult.

Action Selector

We can write [HttpPost] or [HttpGet] to specify the action type

Lets say , we want to give hiphens in the action name, the action mathod cannot have hiphents in name. Or lets just siply say we want to give a different name to the actio than the actual action method name.

We do it like this

Action Result

The following classes derive from the ActionResult class and can be used as an action method’s return type:

  • ViewResult: Used to return a view to render HTML in the browser. This is the most                 common ActionResult.
  • PartialViewResult: Similar to ViewResult, it returns a partial view.
  • ContentResult: Used to return any type of content. By default, it is used to return plain text, but the actual content type can be explicitly defined (e.g. text/xml).
  • EmptyResult: This is the equivalent of a void method—it is by definition an action result object, but it does nothing.
  • FileResult: Used to return binary content (e.g., if you want to download a file).
  • HttpUnauthorizedResult: You can return an HttpUnauthorizedResult object when the request tries to access restricted content that, for example, is not available to anonymous users. The browser is then redirected to the login page.
  • JavaScriptResult: Used to return JavaScript code.
  • JsonResult: Used to return an object in JavaScript Object Notation (JSON) format.
  • RedirectResult: Used to perform an HTTP redirect to another URL. You can define it as a temporary (code 302) or permanent (code 301) HTTP redirect.
  • RedirectToRouteResult: used to perform an HTTP redirect, but to a specific route rather than a URL.

No comments:

Post a Comment