Thursday 10 December 2009

ASP.NET MVC 2 Areas in a Converted ASP.NET Application

I recently converted our ASP.NET Application to ASP.NET MVC 2.  This allows me to run ASP.NET pages along side ASP.NET MVC pages without any trouble.  Well we have a big new functionality project coming up and it would be a shame not to take advantage of the ASP.NET MVC 2 areas.  The areas  tutorial on MSDN is actually extremely good showing all you need to know on how to setup areas, but it won’t show you how to set it up in a converted ASP.NET web app. 

It’s not too much different really, just a small change.  In the step “Enabling the Custom Build Step for MVC Areas Projects” you’re asked to un-commend some information from the csproj files.  The ASP.NET application does not have this  section.  Fortunately you can jsut copy and paste it in after the already commented out “AfterBuild” section.  It’s pasted below, ready for the master web app.

   1: <!-- To enable MVC area subproject support, uncomment the following two lines: -->
   2:   <UsingTask TaskName="Microsoft.Web.Mvc.Build.CreateAreaManifest" AssemblyName="Microsoft.Web.Mvc.Build, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
   3:   <UsingTask TaskName="Microsoft.Web.Mvc.Build.CopyAreaManifests" AssemblyName="Microsoft.Web.Mvc.Build, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
   4:  
   5:   <Target Name="AfterBuild" DependsOnTargets="AfterBuildCompiler">
   6:     <PropertyGroup>
   7:       <AreasManifestDir>$(ProjectDir)\..\Manifests</AreasManifestDir>
   8:     </PropertyGroup>
   9:     <!-- If this is an area child project, uncomment the following line:
  10:     <CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Child" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
  11:     -->
  12:     <!-- If this is an area parent project, uncomment the following lines: -->
  13:     <CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Parent" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
  14:     <CopyAreaManifests ManifestPath="$(AreasManifestDir)" CrossCopy="false" RenameViews="true" />
  15:  
  16:   </Target>
  17:   <Target Name="AfterBuildCompiler" Condition="'$(MvcBuildViews)'=='true'">
  18:     <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
  19:   </Target>

Take all of it, you’ll need it.  Once you’ve done that and reloaded the site (be sure to set your startup project again) you’re good to go.  Also bear in mind that your project should have the same name as the area path, if you want http://localhost/MyNewArea/Controller/Action/id then you should call your project MyNewArea not MyNamespace.MyNewArea.  When you build the views are placed into the Areas folder of the parent project, they are placed under the folder which is the namespace of the project.  There could be a way around this, let me know if you have one, but it’s pretty easy to keep the namespace simple.

3 comments:

OmiD said...

hi
i do any of steps in

http://msdn.microsoft.com/en-us/library/ee307987%28VS.100%29.aspx#building_and_running_the_application

until "Enabling the Custom Build Step for MVC Areas Projects"
but when i want to edit "MvcAreasMultiProject.csproj" the lines

Shannon Meason said...

Do you have any suggestions for the latest release? These build steps have not only been commented out, they have been removed from the build assemblies.

Odd said...

The post is no longer relevant as with more recent versions you just add your areas in the regular ASP.NET MVC way.