Your browser does not support JavaScript! Please enable script of your browser.
 
Coding is Rhyme
Handy Tricks For Beginners & Professionals
ASP.NET MVC 5, 4 - Tutorial, Article Pdf, Training, Code Snippets, Reference Manual, Model, View, Controller, Routing System, Unobtrusive Validation, Html Helpers, Razor Engine, Data Annotations, Dependancy Injection, Database Access, Security, Configuration and Deployment

Understading Controllers and Actions in MVC Razor

Posted By : Shailendra Chauhan, 03 Nov 2012
Updated On : 11 Jun 2014
  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.

About the Author
Shailendra Chauhan works as Sr. Software Engineer at reputed MNC and has more than 5 years of hand over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of www.dotnet-tricks.com and www.dotnetinterviewtricks.com blogs. He is an author of books ASP.NET MVC Interview Questions and Answers & LINQ Interview Questions and Answers.
He loves 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...
 
 
Recommended Articles!
 
 
Search Articles
 
 
Subscribe & follow Us
 
 
Protected by Copyscape Web Plagiarism Check
 
Free ASP.NET MVC Interview Book
Learn In Hindi
Browse By Category
 
Recent Articles
 
Popular Articles
 
Like us on Facebook
 
Featured AD
Join our Training programs in Delhi/Noida on ASP.NET MVC, WCF, EF, jQuery - call us    +91-9871749695