共用方式為


Deploying an ASP.NET HttpHandler to SharePoint 2010

Got a cool question in email today… how to deploy an HttpHandler to SharePoint 2010 that uses code-behind.  The answer is that you don’t really use code-behind like you would in an ASP.NET application where the code and assembly are in the same folder.  With SharePoint, you need to deploy your code to the Global Assembly Cache (GAC).

You can create the code for an HttpHandler very easily in Visual Studio.  I am going to steal borrow some code from Ted Pattison’s walkthrough, “Creating a Custom HttpHandler in Windows SharePoint Services 3.0” on MSDN (includes a video as well).

 using System;
using System.Web;
using Microsoft.SharePoint;

namespace ASHXHandlerDemo
{
    public class HelloHttpHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }
 
        
        public void ProcessRequest(HttpContext context)
        {
            SPSite siteColl = SPContext.Current.Site;
            SPWeb site = SPContext.Current.Web;
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello HttpHandler from the site " +
                                   site.Title +
                                   " at " +
                                   site.Url);
        }
    }
}

The question is how to call that code?  When an HTTP request comes in, how do you map that request to this code? 

The easiest way is to create a .ASHX file and deploy it to the LAYOUTS directory in the SharePoint root (aka “14 hive”).  To do this, right-click your SharePoint project in Visual Studio 2010 and choose “Add SharePoint Layouts Mapped Folder”. 

image

In that newly created folder, add a new file with a .ASHX extension with the following contents.  The .ASHX file just points to the code in the GAC using the fully-qualified assembly name.

 <%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="ASHXHandlerDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=684a624f7b7a49ae" %>
<%@ WebHandler Language="C#"  Class="ASHXHandlerDemo.HelloHttpHandler" %>

The solution looks like this in Visual Studio 2010.

image

When you build and deploy your solution from Visual Studio, the code is compiled into an assembly (a .DLL) and registered in the Global Assembly Cache.  The output is:

image

For More Information

Creating a Custom HttpHandler in Windows SharePoint Services 3.0

Consuming SharePoint Lists via AJAX (note that SharePoint 2010 adds the client OM now, but this link is given as an example of using an HttpHandler)

ASP.NET Dynamic Data and Displaying Images with a Custom Field Template

Comments

  • Anonymous
    August 04, 2010
    Awesome as always Kirk!  I ran into the business scenario where we needed to handle urls user had previously bookmarked and of course the HTTPHandler was the solution :)

  • Anonymous
    November 16, 2014
    Is there a way to make a cross-domain request to this service? I've tried adding context.Response.AddHeader("Access-Control-Allow-Origin", "*") but no love. Do I need to modify the web.config? Thanks