Using the ASP.NET MVC framework with Visual Web Developer Express

If you try to use the new ASP.NET MVC framework with Visual Web Developer Express, there are some manual steps you have to take in order to get up and running.

Select “New Web Site” and pick “ASP.NET 3.5 Extensions Web Site”.

The example web.config I saw added the following lines in the “system.web”/”pages” section. But I’m not sure if they are necessary. I just added them as a cargo cult programming convention at this point.

            <namespaces>
                <add namespace="System.Web.Mvc"/>
                <add namespace="System.Linq"/>
            </namespaces>

edit to add: The namespaces element defines a set of namespaces to be imported in every page and serves the same function as the @Import attribute on an aspx page.

You have to create your “Models” and “Controllers” folder in the “App_Code” folder.

You have to manually add global.asax to the site and manually add the default routes. Add this to the “Application_Start” method.

            // Note: Change Url= to Url="[controller].mvc/[action]/[id]" to enable
            //       automatic support on IIS6

            RouteTable.Routes.Add(new Route {
                Url = "[controller]/[action]/[id]",
                Defaults = new { action = "Index", id = (string)null },
                RouteHandler = typeof(MvcRouteHandler)
            });

When adding a new view, make sure it inherits from “ViewPage” not “System.Web.UI.Page”

Remove any content from Default.aspx in the root of the site. It’s just a placeholder to route IIS to the MVC framework.

Add the “using System.Web.Mvc;” line to any MVC page (e.g. Controllers, Views, Model, etc…)

Otherwise the standard MVC framework rules apply.

Phil Haacked will correct me if I missed anything, but following those directions I was able to bring up my default “Index” view and display some text.

  • Pingback: you've been HAACKED

  • http://joshuamcharles.com/blog/ Josh Charles

    Thanks for putting this up! This isn’t quite working for me, though. The ‘namespaces’ element wasn’t getting recognized and the 3.6 version of System.Web.Extensions wasn’t getting loaded into the GAC for some reason. Well, to make a long story short, I’m downloading the VS 2008 Trial version. I have a feeling these problems were unique to my machine because I’ve had concurrent installs of the beta’s with VS 2005, and I don’t think the uninstall of Orca completely properly.

  • http://www.lazycoder.com Scott

    Josh,

    Make sure the namespaces element goes inside of the pages element in the system.web section of your web.config. I put mine in right after the controls section.

    I had the betas installed on my machine as well side-by-side with VS 2005. Make sure to follow the instructions that Scott Guthrie posted for uninstalling the beta bits.
    http://weblogs.asp.net/scottgu/archive/2007/11/20/steps-to-uninstall-vs-2008-beta2-before-installing-the-vs-2008-final-release.aspx

  • http://joshuamcharles.com/blog/ Josh Charles

    Scott, thanks for the additional tip! I continued tinkering with it, and now it’s going a bit better.

    Some clarifications about directory structure:

    – App_Code
    – Controllers
    – HomeController.cs
    – Models
    Home.aspx.cs
    – Views
    – Home
    – Home.aspx

    Basically, in order for everything to work, the Views folder had to be outside the AppCode folder, but the code-behinds for any views had to be inside of the AppCode folder. Granted I’m pretty much a bat flying in a sound-proof room figuring this stuff out, so I’ve probably done something drastically wrong.

    It does work. Now I’m looking at passing information around to see if it really does, or if it’s a trick of my imagination. I have a feeling that what’s really happening is that the framework is generating a class behind the scenes and ignoring the stuff in App_Code.

  • http://joshuamcharles.com/blog/ Josh Charles

    oh…whitespace isn’t preserved. Sorry about that.

  • http://www.lazycoder.com Scott

    but the code-behinds for any views had to be inside of the AppCode folder.

    I didn’t have to put the code-behind in the App_Code directory for my view. But I haven’t done anything more substantial than Response.write something out.

  • Pingback: Christopher Steen

  • http://www.pownce.com/justinkadima/ justinkadima

    The views directory is outside the App_Code (including code behind …like you do with web form) .The controller +models directories go under App_Code.

  • Pingback: rascunho » Blog Archive » links for 2007-12-11

  • Harry Chou

    I made it working according to Josh’s instruction. Just to contribute to your great effort, I created some screenshots:

    1. folder structure
    http://harrychou.com/img/mvc/mvc_01.gif

    2. HomeController.cs
    http://harrychou.com/img/mvc/mvc_02.gif

    3. Index.aspx.cs
    http://harrychou.com/img/mvc/mvc_03.gif

    3. Index.aspx page
    http://harrychou.com/img/mvc/mvc_04.gif

    3. Global.asax
    http://harrychou.com/img/mvc/mvc_05.gif

    3. Web.config
    http://harrychou.com/img/mvc/mvc_06.gif

    And, Default.aspx is just an empty page ….

    After all this, I can see my page using http://localhost:49576/FirstWebSite/Home

    Very cool. Thank you guys …

  • Pingback: Erster Eindruck von der ASP.NET MVC mit dem Visual Web Developer Express | Code-Inside Blog

  • http://www.pksoftware.net/blog/ Paul Kohler

    Thanks for the tip – I wrapped it up into a project template for VWD express:

    http://www.pksoftware.net/blog/2007/12/24/An+MVC+Project+Template+For+Visual+Web+Developer+2008+Express.aspx

  • Pingback: Oneda

  • Pingback: ASP.NET MVC Archived Buzz, Page 1

  • Pingback: ASP.NET MVC Archived Buzz, Page 1

  • http://www.hireaspnetdeveloper.com/hire-asp-net-web-developer-programmer.html Offshote ASP.net web programmer

    Beautiful Post……..Thanks dude!