Custom Search

August 27, 2008

Creating Controller ASP.Net MVC Framework

If you have installed the ASP.NET MVC Framework correctly, you would probably have an easy time on creating a new MVC Controller. (Quick Link) However, there are times that you need to create new controllers without using the wizard. One of the most particular reason is... you just want to create it your self. So, if you are one of those guys that are not lazy or just curious, then you are in the right site.


Creating Controller

First of all, you need to add a code file in your project.

In the file you just created add this code:

public class ProductController : Controller
{
public ActionResult Index()
{
//Add action logic here
return View();
}
}


For those who have very little knowledge on creating MVC Controller, don't worry. I'll explain it to you.


public class ProductController : Controller
{
public ActionResult Index(int parameter1, string parameter2)
{
//Add action logic here
return View();
}
}

Color Coded (above) Explanation:

  • RED: The Controller Name. In making a controller you must keep this format, ControllerName + Controller.
    Examples:
    Controller NameController Class Name
    ProfileProfileController
    Department
    DepartmentController
    RequestRequestController


    You should also remember that if you create a view on this controller you must create a new folder under the views with the same name of your Controller Name.

  • Green: The Action Method. This is the method in which your logic will be implemented. As default, Action Method Name (with return value of ActionResult) should be always the same as the ViewPage Name you want to render. If you want to render a ViewPage that is not the same as Action Method's Name, then you should specify it in the View() Method.

    EXAMPLE:
    return View("AnotherViewPage");

    (Click this for the Reference)
  • BLUE: Action Method return value.


ActionResult Return Type

All action methods must return an instance a class that is derived from ActionResult. The ActionResult class is the base for all action results. However, there are different action result types, depending on the action that the action method is taking. For example, the most common action is to call the View method. The View method returns an instance of the ViewResult class, which is derived from ActionResult.

The following list shows the built-in action result types.

  • ViewResult. Returned by the View method.

  • RedirectToRouteResult. Returned by the RedirectToAction and RedirectToRoute methods.

  • RedirectResult. Returned by the Redirect method.

  • ContentResult. Returned by the Content method.

  • JsonResult. Return by the Json method.

  • EmptyResult. Returned if the action method must return a null result.

ASP.Net MVC Framework Introduction

MVC stands for Model - View - Controller and is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other.


Model

Used to accessing, retrieving, using, and maintaining data.

View

Used for displaying all the data.

Controller

Used to handle things from View to Model and From Model to View. The interaction between View and Model happens in the Controller.

Release History:

  • ASP.NET MVC Framework ( December 10, 2007)
  • ASP.NET MVC Preview 2 ( March 5, 2008 )
  • ASP.NET MVC Preview 3 ( May 1, 2008 )
  • ASP.NET MVC Preview 4 ( July 16, 2008 )
  • ASP.NET MVC Preview 5 ( August 28, 2008 )
  • ASP.NET MVC Beta ( October 16, 2008 )
  • ASP.NET MVC Release (expected to be released on, January, 2009)

ASP.NET MVC Official Website