This article describes how to add ashx (HttpHandler) in SharePoint 2013/2010 Solutions.
The general handler (HttpHandler) is · NET is one of many Web components, and ashx is its extension. A HttpHandler accepts and processes an HTTP request, analogous to a servlet in Java. Analogy to the need to inherit the HttpServlet class in Java, you need to implement the IHttpHandler interface in. NET, this interface has a isreusable member, a method to implement ProcessRequest (HTTPCONTEXTCTX). The program handles the received HTTP request in the ProcessRequest method. Member IsReusable Specifies whether an instance of this ihttphnadler can be used to handle multiple requests.
The. ASHX program is suitable for producing data formats that are processed by the browser that do not require postback processing, such as for generating dynamic pictures, dynamic text, and so on.
The following article describes how to add ashx (HttpHandler) to a SharePoint 2013/2010 solution:
1. Create a New Farm solution
2. Mapping SharePoint's Layout folder
3. Add a new project
4. Select Application Page
5. Change the TEST.ASHX code to
SharePoint 2013 (note replacing class name)
<%@ Assembly name="$SharePoint. Project.assemblyfullname%><%@ Assemblyname=" Microsoft.SharePoint, version=15.0. 0.0, Culture=neutral, publickeytoken=71e9bce111e9429c%><%@ webhandler language= "C#" class= "Testashx.test"%>
SharePoint 2010 (Note replacing class name):
<%@ Assembly name="$SharePoint. Project.assemblyfullname%><%@ Assemblyname=" Microsoft.SharePoint, version=15.0. 0.0, Culture=neutral, publickeytoken=71e9bce111e9429c%><%@ webhandler language= "C#" class= "Testashx.test"%>
6. Test.ashx.cs Code
Using System;Using Microsoft.SharePoint;Using Microsoft.SharePoint.WebControls;Using System.Web;Namespace testashx{ PublicPartialClass Test:ihttphandler{ PublicBOOL IsReusable { get {ReturnTrue } } Publicvoid ProcessRequest (HttpContext context) { System.Web.Script.Serialization.JavaScriptSerializer Jsonserializer =New System.Web.Script.Serialization.JavaScriptSerializer (); Context. Response.ContentType ="Application/json"; var jsonresult = jsonserializer.serialize (new Josnresult () {name=29}); context. Response.Write (Jsonresult); } } class Josnresult { public string Name {get; set;} public int age {get; set;} } /span>
7. UnLoad the project, edit the project project file, insert it in the PropertyGroup node:
<tokenreplacementfileextensions>ashx</tokenreplacementfileextensions>
8. Reload Project, deploy solution
9. Verification
A. SharePoint 2013 verify that the address is .../_layouts/15/xxx/text.ashx
B. SharePoint 2010 Verify that the address is .../_layouts/xxx/text.ashx
Open the above address to return a JSON object
In the actual application can use the AJAX request TEXT.ASHX to obtain the data, unfolds in the foreground, achieves does not refresh the page to update the data the purpose.
How to add ashx (HttpHandler) in SharePoint 2013/2010 Solutions