Showing posts with label Controller. Show all posts
Showing posts with label Controller. Show all posts

Tuesday, November 10, 2009

Understanding MVC Controllers

In a previous post when took an overview of the Hello World application and located where are the major three elements: Models, Views and Controllers.
Now we're going to explore the MVC Controller in details.
We saw that Controllers respond to browser requests, the controller might return a specific view or redirects to another controller
The following code demonstrates a Controller class for a view called Demo



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Mvc.Ajax;



namespace HelloWorld.Controllers

{

public class DemoController : Controller

{

//

// GET: /Demo/



public ActionResult Index()

{

return View();

}

public ActionResult Page2()

{

return View();

}



}

}





This class has two functions: Index() which returns Index.aspx (located in Views/Demo/Index.aspx) when the user types

[URL]/Demo in the browser.

The second function is Page2() which returns Page2.aspx (located in Views/Demo/Page2.aspx) when the user types [URL]/Demo/Page2 in the browser



The controller exposes Controller Actions, an action is a function on a controller that is invoked when a certain url is entered in the browser.

so when the user types [URL]/Demo/Page2, Page2() function is a controller action on DemoController.cs

The Controller Action must be:


  1. Public: Any function declared as public in a Controller is exposed as Controller Action automatically. So be careful when declaring public methods as they are accessible by the user if the correct URL is typed.

  2. Controller Action can not be overloaded.

  3. Controller Action can not be static.
We saw that the Controller Action returns an object of type ActionResult, so what is the
ActionResul tobject:
The ActionResult can be one of these
  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 these ActionResults inherit from ActionResult base class.

In our Index() Method we saw that it returns a Viewresult by calling the View() method.
Normally you don't return an ActionResult directly instead you call one of these 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.

Consider the following functions



public string Index()
{
return "Hello World";
}
public DateTime Page2()
{
return DateTime.Now;
}

The Index method just returns a string "Hello World"
The Page2() method retuns the current date and time as a plain text

Saturday, November 7, 2009

MVC Anatomy

In a previous post we saw how to create a Hello World MVC application.
Now we're going to perform an anatomy to this application to know what each part of the application exactly means.

First: Where is the Model, the View and the Controller ?
MVC Controller:
In tradotional asp.net the url you type corresponds to a page on the server, so when you request yoursite/abou.aspx there must be a page named about.aspx or a 404- page not found error is thrown.

In MVC it's different, there is no correspondance between the url you type and the pages that exist in the application. rather the url corresponds to a controller action than a page on the server.

The Controller is responsible for the way the user interacts with the application.
it manages the flow control logic of the application. It determines what response to make when the user makes a browser request.

The Controller is a C# class. Our Hello World application contains a controller class named HomeController.cs, It contains only one function named:public string Index()

public string Index()

{
return "Hello World";

}
this function is called when the user types URL /Home/Index in the browser cause as we said the controller functions respond to a user request.
Final thing to note that any public method contained in a controller is exposed as a controller action, meaning that it can be invoked by any user by typing it's name in the browser.
so if you have a function declared public as the following:

public void SomeFunction()







{



// Some Code



}

Any user can call this function by typing URL/Home/SomeFunction so be careful when declaring public functions in Controllers.

MVC View:
The view is equivalent to a page in t ASP.NET. It contains HTML markup that appears on the browser.
so for example if you want to create a page called demo , in Views folder create a subfolder named demo then in this subfolder create an aspx called Index.
then go to the Controllers folder and create a new controller with the name

DemoController.cs

in DemoController.cs add the following code


public ActionResult Index()
{
return View();
}

so when the user types url/demo the demo page is displayed
Notice that you must create the controller class in this form [ViewName]Controller.cs
MVC Model:
The MVC Model Contains you business logic, Validation logic or database access logic.
since that the Hello World application is not that complicated, there are no models in this application
Conclusion:
The view contains the logic for the interface, the Controller contains the logic for redirecting the user correctly to pages and respond to the user requests. And the Model contains the business or database access logic.
The Controller should contain minimal amount of code to handle user requests only.
If the Controller contains large bulks of code, consider removing some of them to a Model class in the Models folder