Wednesday, 3 August 2011

Adding MVC 3 to an asp.net web site

OK, I've been off the blog for a while with things being very busy at work (some interesting changes coming up on public.presalesadvisor.com), but I'm back (for now at least).

I've been starting to tool up on MVC, and while I won't cover that right now as there's no shortage of coverage on that, I wanted to cover one bit that seems slightly lacking in coverage, which is specifically adding MVC 3 to ASP.Net Websites (not web projects).

The most excellent Mr Hanselman has already done the vast majority of the work for anyone wanting to add mvc to asp.net (and in fact pretty much all of it if you're using a web application) with his AddMvc3ToWebForms Nuget Package

There are however a couple more steps needed to get it working with a web site rather than web project

1) Install the nuget package as normal, either by selecting AddMvc3ToWebForms from the online gallery in Visual Studio's "Add Library Package Reference" option (Tools > Library Package Manager > Add Library Package Reference), or by typing the command Install-Package AddMvc3ToWebForms in the Package Manager Console (Tools > Library Package Manager > Package Manager Console)

When I ran this I got an error at the end but as far as I can tell, this isn't actually a problem, but caused by it not having a csproj file to work with:

 PM> Install-Package AddMvc3ToWebForms
'WebActivator (≥ 1.3)' not installed. Attempting to retrieve dependency from source...
Done.
Successfully installed 'WebActivator 1.3.2'.
Successfully installed 'AddMvc3ToWebForms 0.9.1'.
Successfully added 'WebActivator 1.3.2' to extranet.
Successfully added 'AddMvc3ToWebForms 0.9.1' to extranet.
Install-Package : Method invocation failed because [System.__ComObject] doesn't contain a method named 'Add'.
At line:1 char:16
+ Install-Package <<<<  AddMvc3ToWebForms
    + CategoryInfo          : NotSpecified: (:) [Install-Package], RuntimeException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand


If you try and compile now it will fail, complaing of not being able to find MvcApplication1. This is because the view (cshtml) files it creates reference a standard namespace (MvcApplication1) that doesn't correspond to the ASP namespace in the created model/controller files. There is also the additional issue that in a web site the 'M&C' files should live under App_Code not in the rool directory.

2a) If you aren't interested in the initial files created by the package then you could simply create Models and Controllers directories under App_Code, delete the ones in the site root, and empty out the Views directory, then get on with your own coding

2b) If you do want to keep the Account/Home/Shared files that have been created then move the Models and Controllers files to App_Code

3) To resolve the namespace issue open up ChangePassword.cshtml, LogOn.cshtml, Register.cshtml in Views\Account and remove the 'MvcApplication1.' from the @model reference at the top

You should now be ready to roll - Enjoy

2 comments:

  1. When doing this in Webforms Application, we can get MVC specific menu by editing the csproj file. What about WebSites?

    ReplyDelete
    Replies
    1. Sorry for the delay in replying - been a case of head down and getting on with work of late.

      I'll have a look into this, but the big problem you've got is that a WebSite wouldn't have a project file to modify. An interesting one though.

      Delete