10Feb

Controllers and Actions in MVC

Every request that comes to our application is handled by a controller. The controller is free to handle the request any way it wants, as long as it doesn’t belong into the areas of responsibility of the model and view. This means that we do not put business or data storage logic into a controller, nor can we generate user interfaces.

In the ASP.NET MVC Framework, controllers are .NET classes that contain the logic required to handle a request. The role of the controller is to encapsulate your application logic. This means that controllers are responsible for processing incoming requests, performing operations on the domain model, and selecting views to render to the user.

In the MVC Framework, controller classes must implement the IController interface from the System.Web.Mvc namespace.

The MVC Framework is very flexible. You can implement the IController interface to create any kind of request handling and result generation you require.

 

The Controller class provides three key features:

Action methods: A controller’s behavior is partitioned into multiple methods (instead of having just one single Execute() method). Each action method is exposed on a different URL and is invoked with parameters extracted from the incoming request.

Action results: You can return an object describing the result of an action (for example, rendering a view, or redirecting to a different URL or action method), which is then carried out on your behalf. The separation between specifying results and executing them simplifies unit testing.

Filters: You can encapsulate reusable behaviors as filters, and then tag each behavior onto one or more controllers or action methods by putting an [Attribute] in your source code.

 

Controller Actions

A Controller exposes Controller Actions. An action is a method on a controller that gets called when you enter a particular URL in the browser address bar. For example let us imagine we make a request for the following URL-

 

http://localhost/Employee/Index

 

In this case the Index method is called on the Employee Controller class. The Index method is an example of a controller action.

A controller action must be a public method of a controller class. By default C# methods are private. We should note that any public method we add to the controller class is exposed as a controller action automatically.

Action Results

Controller actions return something known as Action Results.

ASP.NET Framework supports several 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).

Let us see this through an example-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
 public class EmployeeController : Controller
 {
  public ActionResult Index()
 {
     // Add action logic here
   return View();
  }
  }
}

In the above example the Index action returns a view result. Note that to return a view result the View() method is called. In the same way we call different methods to return any of the action results. We cannot return Action results directly.

Few built in Action results along with their methods is given below-

ViewResult   - View()

PartialViewResult -  PartialView()

RedirectToRouteResult -RedirectToRoute(),RedirectToRoutePermanent(),RedirectToAction()

RedirectResult - Redirect(),RedirectPermanent()

HttpNotFoundResult - HttpNotFound()

                                                 

                                

 

 

Related

Token Based Authentication for Web API's

Securing ASP.NET Web API using Custom Token Based AuthenticationProviding a security to the Web API&...

Read More >

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

Asynchronous actions in MVC 5

What does it mean?Asynchronous actions allow developers to handle more concurrent requests and can b...

Read More >

ELMAH Integration in ASP.NET MVC Application

ELMAH(Error Logging Modules And Handlers)What is ELMAH?ELMAH (Error Logging Modules and Handlers) is...

Read More >

Unit testing in .NET

Unit Testing – ExplainedA process which involves writing code to verify a system at a lower an...

Read More >

THE RAZOR VIEW ENGINE IN MVC

View Engine is responsible for rendering the view into html form to the browser.ASP.NET MVC includes...

Read More >

ViewData and ViewBag in MVC

ASP.NET MVC provides us two options ViewData and ViewBag for passing data from controller to view.Vi...

Read More >

Creating a Simple Form with Validation Using ASP.NET MVC 4 Empty Template

There are multiple Validation attributes available in MVC. Validation attributes comes from System.C...

Read More >

Brief Journey to Asp.Net MVC Framework

Introduction: ASP.NET MVC is a  framework for building web applications that uses the mode...

Read More >

Share

Post a Comment

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *