In a MVC application the member that gets returned to the client and is visible to the application user is the View. As a user interacts with the MVC application and performs some action to post or submit the form to the server, the request is handled by the controller who know which view to return at the end of the cycle. The code for the controller goes something like this

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace SampleApplication.Controllers

{

  [HandleError]

  public class HomeController : Controller

  {

    public ActionResult Index()

    {

      ViewData["Message"] = "Welcome to ASP.NET MVC!";

 

      return View();

    }

 

    public ActionResult About()

    {

      return View();

    }

  }

}

 

In the code above the controller returns the Index or the About views depending on the controller method which handled the request. Its not necessary that the Index method returns the Index view and the About method returns the About view. We can explicitly specify which view to return from a given controller. For example in the code below the Test controller method returns the Index view.

namespace SampleApplication.Controllers

{

  [HandleError]

  public class HomeController : Controller

  {

    public ActionResult Index()

    {

      ViewData["Message"] = "Welcome to ASP.NET MVC!";

 

      return View();

    }

 

    public ActionResult About()

    {

      return View();

    }

 

    public ActionResult Test()

    {

      ViewData["Message"] = "Index view return from the Test action .";

      return View("Index");

    }

  }

}

Now the next question that comes to mind is that is View the only thing that we can return from a controller method. The answer is no. There are other things that can be returned by the controller methods, for example


Result returned

Description

ViewResult

Returns html and markup

JsonResult

Retruns a json object

JavascriptResult

Returns a javascript. The content type of the response is set to application/javascript

ContentResult

Returns a user defined content type

FileContentResult

Sends binary contents in the response.

FilePathResult

Sends the content of a file to the response

FileStreamResult

Sends a file stream as binary content in the response.

 

 

SampleApplication.rar (260.62 kb)