ASP.NET MVC from scratch

ASP.NET MVC is Microsoft’s answer to Ruby on Rails. Apart from the Model-View-Controller design pattern it contains the following:

  • URL routing
  • Code generation
  • Convention over configuration
  • Razor markup instead of ASPX (optional)

When you create a new ASP.NET MVC project in Visual Studio you can start with all the code necessary for creating and logging in users. You can also use code generation to generate CRUD interfaces. While this might be nice to have I suspect it isn’t very useful in bigger projects. But my main problem with this is that far too many tutorials will make use of it. I think this makes it harder to understand how the system actually works.

From nothing to a web page

Therefore let us start with an empty ASP.NET MVC 3 application and take it step by step until we have a web page without generating large quantities of code.

First we must set up a route in the Global.asax.cs file for the root of the web site:

routes.MapRoute(
    "Front", // Name
    "", // URL
    new { controller = "Main", action = "Front" }
);

Now if someone goes to our web site the Front action on the Main controller will be called. Here is where the convention over configuration comes in. MVC will look for a method named Front in a class named MainController in the Controllers namespace. This method must return a type derived from ActionResult.

namespace MvcTest.Controllers
{
    public class MainController : Controller
    {
        public ContentResult Front()
        {
            return Content("Front page", "text/plain");
        }
    }
}

This is one of the biggest strengths with MVC in my opinion. Every request to the web server can be routed to a method in a controller. Then you can return anything you want from there, there are many types of results that you can return.

The front page now works. But you probably want to return a web page and not just text. First change the return value of the method:

public ViewResult Front()
{
    return View();
}

Yet again the convention comes up. MVC will now search for an appropriate file to use. If you run the application it will show an error message with a list of the file it searches for. We will use the following file because we use Razor and C#: Views/Main/Front.cshtml

@{
    ViewBag.Title = "Front page";
}
<h2>Front page</h2>

The ViewBag that we here use to set the title is an object that is available both in the controller and the view. It can be used to pass information from the controller to the view as in the following example:

public ViewResult Front()
{
    ViewBag.Greeting = "Hello world";
    return View();
}

 

@{
    ViewBag.Title = "Front page";
}
<h2>@ViewBag.Greeting</h2>

The last part is an example of the Razor syntax. This is used instead of the ASPX syntax used in ASP.NET Web Forms.

In practice you will use the ViewBag sparingly. Instead you can create classes in the Models namespace. These classes can either be used only for moving data between controllers and views or they can be used to represent tables in a database.

Advertisements

One thought on “ASP.NET MVC from scratch

  1. Pingback: Let the client do the work | Pseudorandom Knowledge

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s