默認,在用vs2013開發SharePoint項目時,vs沒有提供一般應用程序(.ashx)的項目模板,本文解決此問題。
- 以管理員身份啟動vs2013,創建一個"SharePoint 2013 - 空項目",名稱我保持默認:SharePointProject2。
- 選擇"部署為場解決方案",結果如下:
-
右擊"SharePointProject2",選擇"添加"----SharePoint 的"Layouts"映射文件夾,結果如下:
-
右擊Layouts下的SharePointProject2文件夾,選擇添加---新建項,選擇"應用程序頁(僅場解決方案)",修改名稱,注意文件擴展名改為:ashx
效果:
- 右擊"ApplicationPage1.ashx.designer.cs"選擇刪除,此文件不是我們必需的。
- 修改ApplicationPage1.ashx為如下內容(留意紅框中的內容):
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ WebHandler Class="$SharePoint.Type.be94b0d0-ca37-4783-b8e9-06ba0477a22f.FullName$" %>
- 修改ApplicationPage1.ashx.cs內容如下(注意紅框中的GUID和上面的GUID要保持一致):
using System;
using System.Web;
using System.Runtime.InteropServices;
namespace SharePointProject2.Layouts.SharePointProject2
{
[Guid("be94b0d0-ca37-4783-b8e9-06ba0477a22f")]
public partial class ApplicationPage1 : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { throw new NotImplementedException(); }
}
public void ProcessRequest(HttpContext context)
{
//throw new NotImplementedException();
context.Response.Write("123");
}
#endregion
}
}
- 右擊項目名"SharePointProject2",選擇"卸載項目",如有提示,請選擇"是"來保存項目
- 再次右擊項目名"SharePointProject2",選擇"編輯SharePointProject2.csproj"
-
修改如下,然后保存:
在PropertyGroup節點下面,添加如下:
<TokenReplacementFileExtensions>ashx</TokenReplacementFileExtensions>
十一.右擊項目"SharePointProject2",選擇"重新加載項目",注意要關閉我們打開的SharePointProject2.csproj文件。
十二.右擊項目"SharePointProject2",選擇"部署"。打開瀏覽器就可以瀏覽了。
關於GUID的生成,請在vs里,選擇"工具"----"創建GUID"
關於上面的"六","七"步,如果不想使用GUID的形式,也可以使用如下方式(去掉紅星划掉的部分):
即:把ashx中的Class改為"命名空間.類名"的格式。
如遇報錯,請
右擊"ApplicationPage1.ashx"選擇"屬性",然后把"生成操作"改為"內容"。
右擊"ApplicationPage1.ashx.cs" 選擇"屬性",然后把"生成操作"改為"編譯"。
關於如何在Visual Studio 2010的SharePoint項目中添加一般應用程序(.ashx),建議安裝vs2010插件cks(http://cksdev.codeplex.com/).
當然也可以按上面的方法進行,執行把"六"中的前兩行代碼改為:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
參考:
https://cann0nf0dder.wordpress.com/2013/05/22/creating-generic-httphandler-in-sharepoint/
https://peakfinders.blogspot.jp/2015/03/adding-and-deploying-generic-handlers.html