A one file Asp Net MVC3 Hello World app [beginners]

When I first started with MVC3 I got the impression that even the most simple application requires a lot of work (and a lot of files).
That is not correct. MVC3 can also be used in a much lighter way than many of the tutorials show.

Edit : check out the Really empty MVC3 template by Phil Haack.

The “R” in MVC

It’s not in the M, the V or the C, but really in the R (the router) where the fun begins in an MVC3 application. The router takes care of the actual http request and responds with the help of a choosen function (controller). If we for example were to to serve an empty url request by the Index() function in the Main class, we would need to add a route like this:

RouteTable.Routes.MapRoute("WhateverName", "", new { controller = "Main", action = "Index" });

The first parameter is a name for the route (it seems to be possible to leave that one empty), the second parameter is for the url request to respond to and the third parameter is an anonymous object which defines two values, one for which “Controller” to use and the other for which “Action” function to use. The controller is really a class inherited from the Controller base class. The action function in the controller class returns the response data or redirects the request to another location.

With the code above our application to listens to requests for a path-less request to our site and runs the MainController.Index() – function, which could be as simple as this:

public class MainController : Controller
{
    public ActionResult Index()
    {
        return Content("Hello World");
    }
}

By placing this code in the Global.asax.cs file we’ve just made ourselves a one file MVC3-application.

A sligthly more advanced step could be to handle not only the empty url request but also /one /and/two url’s. Here’s what a complete one file MVC3-app could look like in that case:

using System;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication3
{
    public class MainController : Controller
    {
        public ActionResult Index(string UrlData0, string UrlData1)
        {
            //return as Json:
            return Json(new { x = UrlData0, y = UrlData1 }, JsonRequestBehavior.AllowGet);

            //return as String: 
            //return Content(String.Format("Hello world from UrlData0 {0} and UrlData1 {1}", UrlData0, UrlData1));
        }
    }
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapRoute("", "", new { controller = "Main", action = "Index" });
            RouteTable.Routes.MapRoute("", "{UrlData0}", new { controller = "Main", action = "Index" });
            RouteTable.Routes.MapRoute("", "{UrlData0}/{UrlData1}", new { controller = "Main", action = "Index" });
        }
    }
}

To test this – create an “empty” MVC3 application within Visual Studio and then delete all files and folders but the ones in /bin, the Global.asax and the Web.config. (Okay – “one file” is not entirely correct). Replace the code in your Global.asax.cs with the code above. Then run the application and open some url’s:

What we have now is what, no model, no real view but rather an RC – application ?

Follow ups:

Adding a Razor View to our Hello World MVC3-app

Making the MVC3 Model independent of data source (using Ninject)

Happy coding!

Alternative headers for this article – “low ceremony MVC3” – “coming to MVC3 with WebPages / Nancy / Sinatra background”

Advertisements

One thought on “A one file Asp Net MVC3 Hello World app [beginners]

Leave a Reply

Please log in using one of these methods to post your comment:

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