The following sections outline the steps required to
integrate an ASP.NET MVC 2 site with a Sharepoint Publishing Site. Sharepoint
2007 and 2010 are both built upon the .NET framework 3.5. This restricts the
ASP.NET MVC 2 site to also use this version of the framework.
Creating the ASP.NET MVC 2 Site
In this example we are going to use an MVC site which was
created using the ASP.NET MVC 2 Web Application template within Visual Studio
2008.

Once created and fired up we get the default MVC home page.

This example uses an empty Sharepoint Publishing Site.
Sharepoint professionals will be familiar with the following page.

Configuring the IIS virtual
directory
Our MVC site will run as an IIS virtual directory directly
under the Sharepoint IIS web site
In this instance we are going to name the virtual directory
‘MVC’. This means that any request starting with http:[hostname]/MVC/ will be
served by the MVC site.
We set the ‘web site content directory path’ to the web site
path of the visual studio MVC project. Ensure it is set to ‘Run scripts’.
We now have a standard ASP.NET MVC 2 site sitting under our
host Sharepoint web site, called ‘SampleMVC’

By default this new virtual directory will use the same
application pool as the host Sharepoint application. This will allow the
ASP.NET MVC site to natively access the Sharepoint SPContext object.
Referencing Sharepoint from the
MVC project
In order to expose Sharepoint functionality within our
ASP.NET MVC site, the Microsoft.Sharepoint.dll assembly is referenced by the
ASP.NET MVC web application
For 64bit instances of Sharepoint, referencing this assembly
will cause a conflict within Visual Studio when viewing .aspx files. You will get
the following error regarding an attempt to load a program with an incorrect
format.

This has the effect of turning off all intellisense
associated with .aspx files.
The Microsoft.Sharepoint.dll assembly has a dependency on
the Microsoft.Office.Server.Search.dll assembly. This assembly happens to be
compiled as AMD64 not MSIL.
By default Visual Studio uses the ASP.NET development server
application at design time to parse .aspx files. It is this 32bit application
which conflicts with the 64bit references. This is not a problem at run time as
your IIS instance will also run as a 64bit application
This can be resolved by removing the physical Sharepoint
assemblies from the project output directories. Since these assemblies are
re-created on every build, the following visual studio project post-build event
script is your friend here.
cd$(OutDir)
del Microsoft.Office.Server.Search.dll
del Microsoft.Sharepoint*
Configuring the Sharepoint site
web.config
By default the Sharepoint application domain is not aware
that it will be required to run an ASP.NET MVC site. We need to make some
changes to the Sharepoint Publishing Site web.config file to define the Routing
and MVCHttpHandler configuration
Add entry to the httpHandlers section
<httpHandlers>
<add verb="*" path="*.mvc" validate="false"
type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpHandlers>
Add entry to the httpModules section
<httpModules>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing,
Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" />
</httpModules>
Add entry to the compilation/assemblies section
<compilation batch="false" debug="false">
<assemblies>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
Configuring the ASP.NET MVC 2
site web.config
Since we have configured Routing in the Sharepoint
Publishing Site web.config, we need to remove this from the ASP.NET MVC site
web.config
Remove the UrlRoutingModule entry
<httpModules>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
System.Web.Routing, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
Remove the following ‘add, remove’ entries from
system.webserver/modules
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="UrlRoutingModule"/>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
System.Web.Routing, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
</system.webServer>
Remove the following ‘add, remove’ entries from
system.webserver/handlers
<system.webServer>
<handlers>
<remove name="UrlRoutingHandler"/>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*"
path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler,
System.Web, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
</handlers>
</system.webServer>