Returns the implementation code for JSON using ASP.NET general handler or WebService

  • 2020-05-16 06:38:33
  • OfStack

Download the sample code: http: / / zsharedcode googlecode. com/files/JQueryElementDemo. rar

The contents of this article are as follows:

* prepare
* 1 general processor /ashx
* WebService/asmx preparation

If you want to return by ashx or asmx JSON, you need to reference the assembly System. Web. Extensions. dll, in the NET 3.5, 4.0 has been included. The default for the NET 2.0, 3.0, the need to install ASP. 2.0 AJAX NET, Can in http: / / www. microsoft. com download/en/details aspx? displaylang = en & id = 883 downloads.

1 general processor /ashx

Use 1 general handler to return JSON, similar for different versions of.NET, see handler.ashx code below:

 
<%@ WebHandler Language="C#" Class="handler" %> 
using System; 
using System.Web; 
using System.Web.Script.Serialization; 
using System.Collections.Generic; 
public class handler : IHttpHandler 
{ 
public void ProcessRequest(HttpContext context) 
{ 
context.Response.ContentType = "text/javascript"; 
context.Response.Cache.SetNoStore ( ); 
string name = context.Request["name"]; 
SortedDictionary<string, object> values = new SortedDictionary<string, object>(); 
values.Add("message", 
string.IsNullOrEmpty(name) ? " John doe " : 
string.Format(" hello  {0}, {1}", name, DateTime.Now)); 
context.Response.Write(new JavaScriptSerializer().Serialize(values)); 
} 
public bool IsReusable 
{ 
get { return false; } 
} 
} 

In the above example, the Serialize method of JavaScriptSerializer class converts the object to the JSON string corresponding to JSON. The converted object is SortedDictionary, which generates {"message": "Hello x, 20 xx xx - xx xx: xx: xx" similar string}. If you need to return an array, you can define object [] to transform. The code also USES context. Response. Cache. SetNoStore (); To let the browser revisit ashx every time it requests it, instead of using the cache.
If you use jQuery, you can use the following function to receive JSON:
 
function(data){ 
alert(data.message); 
} 

WebService/asmx
In different versions of.NET, it is slightly different to access WebService via javascript and return JSON.
.NET 2.0, 3.0 Web.config
 
<?xml version="1.0"?> 
<configuration> 
<system.web> 
<compilation debug="true"> 
<assemblies> 
<add 
assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
</assemblies> 
</compilation> 
<pages/> 
<httpHandlers> 
<remove verb="*" path="*.asmx"/> 
<add verb="*" path="*.asmx" 
type="System.Web.Script.Services.ScriptHandlerFactory" 
validate="false"/> 
</httpHandlers> 
</system.web> 
</configuration> 

.NET 3.5 Web.config
.NET 4.0 Web.config
The above two versions of Web.config can be viewed in Web.3.5.config and Web.4.config in the sample zip.
Below is the webservice. asmx/webservice. cs code:
 
<%@ WebService Language="C#" CodeBehind="~/App_Code/webservice.cs" Class="webservice" %> 
using System; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.Web.Script.Services; 
using System.Web.Script.Serialization; 
using System.Collections.Generic; 
[WebService ( Namespace = "http://tempuri.org/" )] 
[WebServiceBinding ( ConformsTo = WsiProfiles.BasicProfile1_1 )] 
[ScriptService] 
public class webservice : System.Web.Services.WebService 
{ 
[WebMethod] 
[ScriptMethod] 
public SortedDictionary<string, object> Save ( string name ) 
{ 
this.Context.Response.Cache.SetNoStore ( ); 
SortedDictionary<string, object> values = new SortedDictionary<string, object> ( ); 
values.Add ( "message", 
string.IsNullOrEmpty ( name ) ? " John doe " : 
string.Format ( " hello  {0}, {1}", name, DateTime.Now ) ); 
return values; 
} 
} 

Instead of using JavaScriptSerializer to convert an object to an JSON string, you can simply return the object. SortedDictionary is returned in the code above, and in.NET 2.0, 3.0 it will look like {"message": "Hello x, 20xx-xx-xx xx:xx:xx"}, and for.NET 3.5, 4.0 {"d": {"message": "Hello x, 20xx-xx-xx xx:xx:xx"}}, so you can accept JSON in jQuery using the following functions:
 
function(data){ 
alert(data.message); 
} 
function(data){ 
alert(data.d.message); 
} 

JQueryElement sharing is open source code, can be in http: / / code google. com p/zsharedcode wiki/download dll Download page or source code.

Actual process demonstrate: http: / / www tudou. com/programs/view rHCYHX9cmcI /, suggest full screen view.

Welcome to visit panzer open source project, http: / / zsharedcode googlecode. com /, which contains the IEBrowser control carry out various js WebBrowser and jQuery script and recording function and jQueryUI Asp. JQueryElement net controls.

Related articles: