Page 1 of 1

[ASP.Net MVC2] How to use Controllers - basics.

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Posted 04 August 2010 - 11:30 AM

Hi guys, today I'm going to show you what Controllers are in ASP.Net MVC2. I'm also going to explain how to easily leverage them to create dynamic and nice pages with very little cruft.

The Microsoft ASP.Net MVC team have a nice motto that I recently follow as my creed when programming. 'Convention over configuration'. These are words to code by when creating MVC2 applications.

I want to you to open Visual Studio (I'm using 2010 Ultimate) and create a new MVC2 application.

As your project is right now, you can actually run it and see the demo application built into MVC2. So close that browser window, and within visual studio I want you to open the Controllers folder. This is where you should be keeping all of your controllers.

Posted Image

A Controller within MVC2 is what will handle every single one of your requests to your application. It's the one that will decide what View to return and what Action to use. Every single time.

Double click on the HomeController.cs file and let's notice a couple of things.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

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


Any Controller you create for your application will inherit from the Controller abstract class (in fact, you can rightclick->Go To Definition to see it).

I just want to emphasize that a Controller in MVC2 is just a class and is nothing fancy. You can even place a Controller ANYWHERE on your solution and your application will still work, however it's recommended that you always place them in the Controllers folder. Convention over configuration.

Now let's direct our attention to the two methods we have in our Controller.

Index() and About()

Both of those methods return the base class called ActionResult(). In the future I'll make some tutorials describing some of the more specific return Result types, but for now just understand that the ActionResult() is like a one-size fits all type return.

The way a Controller works is it takes the name of the controller, strips out the 'Controller' characters and creates an area called in this case Home

Try running your application and typing in http://localhost:123/home/about

Here's what's happening:

Posted Image


OK, enough theory; let's create our own Controller and have it return something.

Right click on your Controller folder and select Add Controller and call it WorldController:

Posted Image

You should now have this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class WorldController : Controller
    {
        //
        // GET: /World/

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

    }
}


Let's create an Action called Time.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class WorldController : Controller
    {
        //
        // GET: /World/

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

        public ActionResult Time()
        {
            ViewData["time"] = System.DateTime.Now;
            return View();
        }
    }
}


I'm using the ViewData dictionary to save the current date and time so we can invoke it from the View.

Now, right click on the return View() line and select Add View, notice that it automagically fills in the view Name to what we called out Action. Convention over configuration in action! :)

Posted Image

For now just leave everything the way it is and click Add.

OK! Now notice in your solution a World area has been added to the Views folder with a single view called Time in it.

Double-click the Time.aspx file and type this in:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Time
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Time</h2>

    //I added this.
    <p><%= ViewData["time"] %></p>

</asp:Content>


Recompile the entire solution (Ctrl+Shift+B), and run your application.

Enter the following URL and you'll this:

http://localhost:123/world/time

Posted Image

I hope you enjoyed this tutorial. Stay tuned for more!

Is This A Good Question/Topic? 1
  • +

Replies To: [ASP.Net MVC2] How to use Controllers - basics.

#2 Guest_software*


Reputation:

Posted 25 February 2011 - 09:10 AM

View PostSergio Tapia, on 04 August 2010 - 11:30 AM, said:

Hi guys, today I'm going to show you what Controllers are in ASP.Net MVC2. I'm also going to explain how to easily leverage them to create dynamic and nice pages with very little cruft.

The Microsoft ASP.Net MVC team have a nice motto that I recently follow as my creed when programming. 'Convention over configuration'. These are words to code by when creating MVC2 applications.

I want to you to open Visual Studio (I'm using 2010 Ultimate) and create a new MVC2 application.

As your project is right now, you can actually run it and see the demo application built into MVC2. So close that browser window, and within visual studio I want you to open the Controllers folder. This is where you should be keeping all of your controllers.

Posted Image

A Controller within MVC2 is what will handle every single one of your requests to your application. It's the one that will decide what View to return and what Action to use. Every single time.

Double click on the HomeController.cs file and let's notice a couple of things.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

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


Any Controller you create for your application will inherit from the Controller abstract class (in fact, you can rightclick->Go To Definition to see it).

I just want to emphasize that a Controller in MVC2 is just a class and is nothing fancy. You can even place a Controller ANYWHERE on your solution and your application will still work, however it's recommended that you always place them in the Controllers folder. Convention over configuration.

Now let's direct our attention to the two methods we have in our Controller.

Index() and About()

Both of those methods return the base class called ActionResult(). In the future I'll make some tutorials describing some of the more specific return Result types, but for now just understand that the ActionResult() is like a one-size fits all type return.

The way a Controller works is it takes the name of the controller, strips out the 'Controller' characters and creates an area called in this case Home

Try running your application and typing in http://localhost:123/home/about

Here's what's happening:

Posted Image


OK, enough theory; let's create our own Controller and have it return something.

Right click on your Controller folder and select Add Controller and call it WorldController:

Posted Image

You should now have this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class WorldController : Controller
    {
        //
        // GET: /World/

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

    }
}


Let's create an Action called Time.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class WorldController : Controller
    {
        //
        // GET: /World/

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

        public ActionResult Time()
        {
            ViewData["time"] = System.DateTime.Now;
            return View();
        }
    }
}


I'm using the ViewData dictionary to save the current date and time so we can invoke it from the View.

Now, right click on the return View() line and select Add View, notice that it automagically fills in the view Name to what we called out Action. Convention over configuration in action! :)

Posted Image

For now just leave everything the way it is and click Add.

OK! Now notice in your solution a World area has been added to the Views folder with a single view called Time in it.

Double-click the Time.aspx file and type this in:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Time
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Time</h2>

    //I added this.
    <p><%= ViewData["time"] %></p>

</asp:Content>


Recompile the entire solution (Ctrl+Shift+B), and run your application.

Enter the following URL and you'll this:

http://localhost:123/world/time

Posted Image

I hope you enjoyed this tutorial. Stay tuned for more!





What is the purpose of adding the below code in view.aspx ( //I added this.
<p><%= ViewData["time"] %></p> ?
Was This Post Helpful? 0

Page 1 of 1