articles

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

Controller in ASP.NET MVC

Chris Anderson6799 29-Sep-2011

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
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
        publicActionResult 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
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
        publicstring Index()
        {
    return "Hello World";
        }
    }
}

 

The ASP.NET MVC framework supports several types of action results:
  1. ViewResult   -  Represents HTML and markup.
  1. EmptyResult -Represents no result.
  2. RedirectResult - Represents a redirection to a new URL.
  3. JsonResult      -   Represents a JavaScript Object Notation result that can be used in an AJAX application.
  4. JavaScriptResult - Represents a JavaScript script.
  5. ContentResult - Represents a text result.
  6. FileContentResult - Represents a downloadable file (with the binary content).
  7. FilePathResult -  Represents a downloadable file (with a path).
  8. 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.

    Updated 07-Sep-2019
    hi I am software developer at mindstick software pvt. ltd.

    Leave Comment

    Comments

    Liked By