您的位置:首页 > 理论基础 > 计算机网络

Deploying an ASP.NET HttpHandler to SharePoint 2010

2011-11-17 17:16 633 查看
[http://blogs.msdn.com/b/kaevans/archive/2010/08/04/deploying-an-asp-net-httphandler-to-sharepoint-2010.aspx]

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”.





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.





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:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐