Skip to main content

ASP.Net MVC –Understanding Controllers

Controllers


 
MVC controllers are responsible for responding to requests made against an ASP.NET MVC website. Each browser request is mapped to a particular controller.

 
A controller is a class that derives from the base System.Web.Mvc.Controller class. Because a controller inherits from this base class, a controller inherits several useful methods .The following doe depicts a controller

 
  
Figure-1


 
Understanding Controller Actions

 
A controller exposes controller actions. An action is a method on a controller that gets called when a particular URL is entered in the browser address bar.

 
For e.g. consider the following URL

 
http://localhost/TestController/Index/3

 
In the above example, Index () method is called on the TestController class. The Index () method is an example of a controller action.

 
Following are the considerations that need to be made while writing controller actions.

 
• A controller action must be a public method of a controller class. Any public method added to a controller class is exposed as a controller action automatically.


• A method used as a controller action cannot be overloaded.


• A controller action cannot be a static method.

 
Understanding Action Results

 
A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

 
The ASP.NET MVC framework supports following types of action results:

 
  • ViewResult: Represents HTML and markup. 
  • EmptyResult: Represents no result. 
  • RedirectResult: Represents a redirection to a new URL.
  • JsonResult: Represents a JavaScript Object Notation result that can be used in an AJAX application.
  • JavaScriptResult: Represents a JavaScript script.
  • ContentResult: Represents a text result.
  • FileContentResult: Represents a downloadable file (with the binary content).
  • FilePathResult: Represents a downloadable file (with a path).
  • FileStreamResult: Represents a downloadable file (with a file stream).
 All of these action results inherit from the base ActionResult class.

 
Index () action in Figure-1 does not return a ViewResult (). Instead, the View () method of the Controller base class is called. Normally, an action result is not returned directly. Instead, one of the following methods of the Controller base class is called:

 
  • View - Returns a ViewResult action result.
  • Redirect - Returns a RedirectResult action result.
  • RedirectToAction - Returns a RedirectToRouteResult action result.
  • RedirectToRoute - Returns a RedirectToRouteResult action result.
  • Json - Returns a JsonResult action result.
  • JavaScriptResult - Returns a JavaScriptResult.
  • Content - Returns a ContentResult action result.
  • File - Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.
 
 
  Figure-2


 
In Figure-2, depending upon whether the id is null, either a redirection to index action is made or a View is returned.

Figure-3


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, else the ContentResult needs to be exclusively used in the code as depicted in Figure-3.


Hope the details provided here are helpful.

Till next we connect…..



Happy Learning.



Comments

Popular posts from this blog

Authentication using Social Networking portals(Facebook, Gmail, and Yahoo)

There are tons of sites, which offer sign on using the social networking site credentials (Facebook, gtalk, twitter and the list continues).It can be termed a “SINGLE SIGNON” and offers a lot of benefits compared to traditional database authentication approach. However, not storing user credentials in the DB imposes an additional risk. How to track who all logged into the system. Now the question is, which approach to follow. The best approach is to use inbuilt asp.net users for storing the logging info about user activities and using single signon technique for authentication. This article will explore the approach and provide the details of implementation using some third party libraries and customizing it to the requirements. The Authentication will be done using the following networking portals Yahoo  Gmail Facebook    Special thanks to my friend Sumit Khandelwal, for implementation of Facebook part (in fact he did it all!!) Except Facebook, all other ca...

WPF Overview-Part-II

This post is in continuation to the last post. In this Post I’ll be exploring the Dependency Properties Dependency properties are similar to CLR properties with more advanced and complex features. The main difference between the CLR properties and dependency properties is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject . In case this description did not make sense, no need to worry, It will become clear by the time you reach end of this article. How the Value is Resolved in Dependency properties Every time a dependency property is accessed, it internally resolves the value by following the precedence from high to low. It checks if a local value is available, if not, check if a custom style trigger is active and I the similar manner continues until it finds a value. At last the default value is alwa...

HTML-5 Let's get started

Want to see html 5 in action, before actually diving into it, here's your chance. Go to this LINK and see the wonderful features that HTML-5 has to offer. Feeling overwhelmed, feel like getting hands dirty with this new kid on the block? Here's how to get started Hope this was useful.... Till next we connect.... Happy reading.....