JsonHandler.ashx.cs
Dosyayı İndir
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using Newtonsoft.Json;
using EntityFrameworkLibrary;
namespace EntityFrameworkWeb
{
/// <summary>
/// Summary description for JsonHandler
/// </summary>
public class JsonHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
SampleEntities db = new SampleEntities();
//long sampleId = Int64.Parse(context.Request["sampleId"]);
//Sample sample = db.Samples.Single(s => s.SampleId == sampleId);
//context.Response.Write("{");
//context.Response.Write(" Sample{");
//context.Response.Write(" \"sampleId\": " + sample.SampleId);
//context.Response.Write(" \"sampleName\": " + sample.SampleName);
//context.Response.Write(" \"sampleValue\": " + sample.SampleValue);
//context.Response.Write(" }");
//context.Response.Write("}");
//JavaScriptSerializer serializer = new JavaScriptSerializer();
//string json = serializer.Serialize(sample);
//context.Response.Write(json);
// IsReference=true problem
//DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Sample));
//serializer.WriteObject(context.Response.OutputStream,sample);
//Newtonsoft JSON.NET, Web Api
//StringWriter sw = new StringWriter();
//JsonTextWriter jw=new JsonTextWriter(sw);
//JsonSerializer serializer = new JsonSerializer();
//serializer.Serialize(jw, sample);
//string json = sw.ToString();
//context.Response.Write(json);
//string json = JsonConvert.SerializeObject(sample,Formatting.Indented);
//context.Response.Write(json);
long categoryId = Int64.Parse(context.Request["categoryId"]);
Category category = db.Categories.Single(c => c.CategoryId == categoryId);
string json = JsonConvert.SerializeObject(category, Formatting.Indented);
context.Response.Write(json);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Dosyayı İndir