SharePoint项目中创建HttpHandler .

1 引言

SharePoint项目开发过程中,有时会遇到异步请求需求,以下介绍的就是通过HttpHandler实现的异步请求配置过程。

2 操作流程

2.1 新建空SharePoint项目

打开VS,点击文件》新建>>项目,在弹出的窗体中,展开左侧模板的SharePoint>>2010,选择右边的‘空白SharePoint项目’,然后输入项目名称(本例中的项目名称为SPHttpHandlerAPP),点击‘确定’后,选择场解决方案。

2.2 映射Layouts目录

在新建好的项目中,右键项目,选择添加>>SharePoint的‘Layouts’映射文件夹。

2.3 创建Application Page页面

Layouts>>项目名称的目录下,右键添加>>新建项,在弹出的窗体中选择应用程序页,输入页面名称(本例的页面名称为HttpHandlerPage),点击‘添加’。

2.4 删除designer文件

在新建的应用程序页目录下,可以看到有三个文件,删除HttpHandlerPage.aspx.designer.cs文件。

2.5 更改页面文件后缀名

HttpHandlerPage.aspx文件名称改为HttpHandlerPage.ashx

2.6 更改页面后台文件内容

1、删除后台文件内容

2、添加如下图代码,该类继承自IhttpHandler接口。

  1. using System.Web;  
  2. using System.Runtime.InteropServices;  
  3.   
  4. namespace SPHttpHandlerAPP.Layouts.SPHttpHandlerAPP  
  5. {  
  6.     [Guid("1b84b039-aea8-4f7c-a7b2-3b41645b4abe")]  
  7.     public class HttpHandlerPage : IHttpHandler  
  8.     {  
  9.         public bool IsReusable  
  10.         {  
  11.             get { return true; }  
  12.         }  
  13.         public void ProcessRequest(HttpContext context)  
  14.         {  
  15.             context.Response.Clear();  
  16.             context.Response.ContentType = "text/plain";  
  17.             context.Response.Write("liugj");  
  18.         }  
  19.     }  
  20. }  
using System.Web;
using System.Runtime.InteropServices;

namespace SPHttpHandlerAPP.Layouts.SPHttpHandlerAPP
{
    [Guid("1b84b039-aea8-4f7c-a7b2-3b41645b4abe")]
    public class HttpHandlerPage : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.ContentType = "text/plain";
            context.Response.Write("liugj");
        }
    }
}


 

3、选择菜单中的工具>>创建GUID,在弹出的窗口中,‘GUID格式’中选择地5项,点击‘复制’按钮并关闭窗口。

4、将复制的GUID粘贴到后台文件对应的位置中。

5、选中GUID号,按快捷键‘ctrl+u’,将GUID大写转换成小写。

 2.7 更改页面文件内容

生成好GUID后,删除页面文件内容,并添加如图的内容,对应的替换成刚才生成的GUID

  1. <%@ WebHandler Class="$SharePoint.Type.1b84b039-aea8-4f7c-a7b2-3b41645b4abe.AssemblyQualifiedName$" %>  
<%@ WebHandler class="$SharePoint.Type.1b84b039-aea8-4f7c-a7b2-3b41645b4abe.AssemblyQualifiedName$" %>


 

2.8 更改项目的.csproj文件

打开项目对应的物理路径,找到SPHttpHandlerAPP.csproj文件,使用编辑器打开,在对应的标签‘Project>PropertyGroup’下,添加以下标签。目的是IIS能够识别ashx文件并对应的处理。

posted @ 2013-09-22 13:02  至善  阅读(250)  评论(0)    收藏  举报