Home > DeveloperSection > Articles > Controller in ASP.NET MVC

Controller in ASP.NET MVC


ASP.NET MVC ASP.NET MVC 
Ratings:
0 Comment(s)
 4083  View(s)
Rate this:

Controller in ASP.NET MVC


Controller is a heart of the entire MVC architecture. Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views. Controller can access and use model class to pass data to views.MVC controllers are responsible for responding to requests made against an ASP.NET MVC website. Each browser request is mapped to a particular controller. 

Here I am explaining how to add and use Controller in an MVC application.

First add Controller:
Right click on Controller folder in Solution explorer
à Add à
Controller.
Add Controller dialog box will open, name the controller as HomeController as seen below and click on Add button.

Controller in ASP.NET MVC

 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 methods too.

A controller exposes controller actions. An action is a method on a controller that gets called when you enter a particular URL in your browser address bar. For example, imagine that you make a request for the following URL:

http://localhost/Home/Index

In this case, the Index() method is called on the HomeController class. The Index() method is an example of a controller action.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace FisrtMVCApp.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/

 

        public ActionResult Index()

        {

            return View();

        }

    }

}

In this case, when the HomeController.Index() action is invoked, a view is not returned. Instead View, the text "Hello World!" is returned to the browser.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace FisrtMVCApp.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/

 

        public string Index()

        {

    return "Hello World";

        }

    }

}

 

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

  1. ViewResult   à  Represents HTML and markup.
  2. EmptyResult àRepresents no result.
  3. RedirectResult à   Represents a redirection to a new URL.
  4. JsonResult      à   Represents a JavaScript Object Notation result that can be used 
                              in an AJAX application.
  5. JavaScriptResult à Represents a JavaScript script.
  6. ContentResult    à Represents a text result.
  7. FileContentResult à Represents a downloadable file (with the binary content).
  8. FilePathResult à     Represents a downloadable file (with a path).
  9. FileStreamResult à Represents a downloadable file (with a file stream).

All of these action results inherit from the base ActionResult class.

Normally, we do not return an action result directly. Instead, we call one of the following methods of the Controller base class:

  1. View à Returns a ViewResult action result.
  2. Redirect à Returns a RedirectResult action result.
  3. RedirectToAction à Returns a RedirectToRouteResult action result.
  4. RedirectToRoute à Returns a RedirectToRouteResult action result.
  5. Json à Returns a JsonResult action result.
  6. JavaScriptResult à Returns a JavaScriptResult.
  7. Content à Returns a ContentResult action result.
  8. File à Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.

Don't want to miss updates? Please click the below button!

Follow MindStick