ASP.NET MVC – Controllers

Controllers

The controller is responsible for the request made by the browser, each request is mapped to a particular controller.

The controller can return a view or redirect to another controller.

The controller is just a class derived from the base System.Web.Mvc.Controller class.

Controller Actions

Action is a method on a controller which gets called when you enter a URL in the browser address.

A controller action needs to be a public method of a controller class.

Controller action methods cannot be overloaded.

Action Results

A controller returns an action result in response to the browser request.

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).

An action result is not passed directly instead the following is passed:

View – Returns a ViewResult action result.

namespace MvcApplication1.Controllers
{
    public class BookController : Controller
    {
        public ActionResult Index()
        {
            // Add action logic here
            return View();
        }
    }
}

Redirect – Returns a RedirectResult action result.

RedirectToAction – Returns a RedirectToRouteResult action result.

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class CustomerController : Controller
    {
        public ActionResult Details(int? id)
        {
            if (!id.HasValue)
                return RedirectToAction("Index");

            return View();
        }
    }
}

RedirectToRoute – Returns a RedirectToRouteResult action result.

Json – Returns a JsonResult action result.

JavaScriptResult – Returns a JavaScriptResult.

Content – Returns a ContentResult action result.

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class StatusController : Controller
    {
        public ActionResult Index()
        {
            return Content("Hello World!");
        }
    }
}

File – Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.

HttpUnauthorizedResult – forces the user to login.

 

Live as if you were to die tomorrow. Learn as if you were to live forever.. -Mahatma Gandhi

ASP.NET MVC – Introduction

Introduction

ASP.NET MVC is a web development framework that combines the effectiveness and tidiness of model-view-controller (MVC) architecture.

Drawbacks of ASP.NET Webforms

  • View State Weight – large blocks of data transferred between client and server on every request creating slower response time and increasing bandwidth.
  • Page life cycle – connecting client-side events with server-side event handler code, some controls fail during the runtime without any view state errors
  • False sense of separation of concerns – developers were encouraged to mix presentation code with application logic in the same code behind classes
  • Limited control over HTML – server controls render themselves as HTML, sometimes fail to meet Web standards, difficult to access ID attribute with JavaScript
  • Leaky abstraction – difficult to implement custom behaviours as web form tries to hide HTML and HTTP
  • Low testability – tightly coupled architecture is difficult for unit testing also integration test

Importance of MVC Framework

Separation of Concern (SoC)

Is a software concept that separates program into different sections or concerns. By doing the separation each can encapsulate information that can be developed and updated independently.

Loose coupling is an architectural approach in which the designer seeks to limit the amount of inter dependencies between various parts of a system.

By reducing inter dependencies, changes to one area of an application are less likely to affect another area.

Repository pattern

Is highly useful in unit testing because it enables you to substitute the actual data connection with a mocked repository that provides well-known data.

The persistence layer deals with persisting (storing and retrieving) data from a datastore, just like the repository.

When using repository you instantiate the interface rather the class. This enables you to use data connection when doing work on the mock repository during testing.

To implement a repository you can create a global repository for all the data, a repository for each entity or some combination.

Live as if you were to die tomorrow. Learn as if you were to live forever.. -Mahatma Gandhi