Using ASP.NET MVC 2 with Sharepoint Publishing
page 3 of 5
by Martin Bailey
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28849/ 43

Creating the custom HttpApplication

Now that we have configured the Sharepoint Publishing Site to use the Routing Module, it now requires a ‘RegisterRoutes’ method call within the ‘Application_Start’ event.

 

To do this we need to create a custom http application class that inherits Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication.

 

For convenience we can create this class within the MVC project. The following code shows an example of this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Diagnostics.CodeAnalysis;
 
namespace SampleMVC
{
public class CustomHttpApplication : 
Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication
    {
 
[SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
       protected virtual void Application_Start(object sender, 
            EventArgs e)
       {
//We have to call this empty method otherwise we get a 
// System.ArgumentException: virtualPath
//If we inlcude System.Web.Routing, then it expects the following 
//call
            RegisterRoutes(RouteTable.Routes);
 
        }
 
        public static void RegisterRoutes(RouteCollection routes)
        {
            //this method needs to be here.
            //actual routing is configured within the MVC site
        }
 
    }
}

We need to sign the ASP.NET MVC web application assembly and GAC deploy, this allows its usage by the Sharepoint Publishing Site.

Configuring the Sharepoint site Global.asax

We need to amend the global.asax file of the Sharepoint Publishing Site to reference this new custom HttpApplication

 

Replace the default code

<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>

With the following code

<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Assembly Name="SampleMVC, Version=1.0.0.0, Culture=neutral, 
PublicKeyToken=996c39a8508e1e46" %>
<%@ Application Language="C#" Inherits="SampleMVC.CustomHttpApplication" %>

Using SPContext within the ASP.NET MVC 2 application

 

Let’s go and access the Sharepoint application domain from our MVC site. In this example we amend the default HomeController to get the Sharepoint Publishing Site url property

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
            ViewData["Message"= "Welcome to Sharepoint MVC," 
            + "Sharepoint site   url : " 
            + SPContext.Current.Site.Url;
       
            return View();
    }
}

Since, by default, we are running this request under the built in anonymous user, IUSR_[machinename], we need to ensure we have allowed anonymous access within the Sharepoint Publishing Site.

 

If we request the home page, we can see that we have successfully retrieved the site name from the Sharepoint SPContext object

In this example every request made under http://samplemvc/MVC is routed by the MVC framework. We are still support the standard Sharepoint Publishing Site too, so http://samplemvc/Pages/default.aspx still resolves to

Using the ASP.NET MVC 2 framework in this way, unlike conventional Sharepoint Publishing web development, we can amend the View files at run time inside Visual Studio. No re-compilation / application pool reset is needed, a simple page refresh will render the new View.

 

Debugging of the ASP.NET MVC site is done by attaching the Visual Studio debugger to the Sharepoint w3wp.exe process.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 7:44:01 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search