Please enable Javascript to correctly display the contents on Dot Net Tricks!
 
Ready to master latest .NET, JavaScript framework and Mobile development skills?
Join our training programs and take your career to the next level!

Understading Controllers and Actions in MVC Razor

Posted By : Shailendra Chauhan, 03 Nov 2012
Updated On : 11 Jun 2014
Total Views : 122,009   
Version Support : MVC3 & MVC4
 
Keywords : what is controllers and actions in mvc razor,types of controllers actions and helper methods,use of controller, mvc controllers helps

Asp.net MVC Controllers are responsible for controlling the flow of the application execution. When you make a request (means request a page) to MVC applications , a controller is responsible for returning the response to that request. A controller can have one or more actions. A controller action can return different types of action results to a particular request.

Controller

Basically, the controller Receives input like as form values, query strings values etc. from users via the View and perform required operations on the user's inputs with the help of Model and passing the results back to the View. Actually, controllers are classes that have methods or action results and these actions results are called by the routing system to process a particular request.

Creating a Controller

For creating a controller, do right-click on the Controller folder in your mvc application and select the menu option Add, Controller. After selection the Add Controller dialog is being displayed as shown in fig. Now enter your controller name before Controller word.

In this way you have successfully created your controller as shown in fig.

Controller Actions and Helper Methods

Controller actions are methods defined in the controller class and responsible to perform required operations on the user's inputs like as form values, query strings values etc. with the help of Model and passing the results back to the View. Asp.net MVC has the following Built-in ActionResults Type and Helper methods :

  1. ViewResult

    Returns a ViewResult which renders the specified or default view by using controller View() helper method.

  2. PartialViewResult

    Returns a PartialViewResult which renders the specified or default partial view (means a view without its layout) by using controller PartialView() helper method.

  3. RedirectResult

    Returns a RedirectResult which Issues an HTTP 301 or 302 redirection to a specific URL by using controller Redirect() helper method.

  4. RedirectToRouteResult

    Returns a RedirectToRouteResult which Issues an HTTP 301 or 302 redirection to an action method or specific route entry by using controller RedirectToAction(), RedirectToActionPermanent(), RedirectToRoute(), RedirectToRoutePermanent() helper methods.

  5. ContentResult

    Returns a ContentResult which renders raw text like as "Hello, DotNet Tricks!" by using controller Content() helper method.

  6. JsonResult

    Returns a JsonResult which serializes an object in JSON format ( like as "{ "Message": Hello, World! }") and renders it by using controller Json() helper method.

  7. JavaScriptResult

    Returns a JavaScriptResult which renders a snippet of JavaScript code like as "function hello() { alert(Hello, World!); }" by using controller JavaScript() helper method. This is used only in AJAX scenarios.

  8. FileResult

    Returns a FileResult which renders the contents of a file like as PDF, DOC, Excel etc. by using controller File() helper method.

  9. EmptyResult

    Returns no result returned by an action. This has no controller helper method.

  10. HttpNotFoundResult

    Returns an HttpNotFoundResult which renders a 404 HTTP Status Code response by using controller HttpNotFound() helper method.

  11. HttpUnauthorizedResult

    Returns an HttpUnauthorizedResult which renders a 401 HTTP Status Code(means "not authorized") response. This has no controller helper method. This is used for authentication (forms authentication or Windows authentication) to ask the user to log in.

  12. HttpStatusCodeResult

    Returns an HttpStatusCodeResult which renders a specified HTTP code response. This has no controller helper method.

What do you think?

I hope you will enjoy these tricks while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

 
Recommended Articles!
 
About the Author
Hey! I'm Shailendra Chauhan author, developer with more than 5 years of hand over Microsoft .NET technologies. I am a .NET Consultant, founder & chief editor of www.dotnet-tricks.com and www.webgeekschool.com. I am author of books ASP.NET MVC Interview Questions and Answers & LINQ Interview Questions and Answers.
I love to work with web applications and mobile apps using Microsoft technology including ASP.NET, MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery, jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...
 
Free Interview Books
 
Browse By Category
Learn In Hindi
 
 
Like us on Facebook