public class GenericHandler : IHttpHandler
{
public class ASSystem
{
public string SID { get; set; }
public string Description { get; set; }
public string SystemName { get; set; }
}
public class ErrorObj
{
public string ErrorMessage { get; set; }
}
public void ProcessRequest(HttpContext context)
{
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
string query = HttpContext.Current.Request.QueryString["SID"];
SOFAEntities ctx = new SOFAEntities();
JavaScriptSerializer serializer = new JavaScriptSerializer();
try
{
AS_SYSTEM system = ctx.AS_SYSTEM.Where(s => s.SYSTEM_ID == query).First() as AS_SYSTEM;
if (system != null)
{
ASSystem sys = new ASSystem() { SID = system.SYSTEM_ID, Description = system.DESCRIPTION, SystemName = system.SYSTEM_NAME };
HttpContext.Current.Response.Write(serializer.Serialize(sys));
}
}
catch (Exception e)
{
HttpContext.Current.Response.Write(serializer.Serialize(new ErrorObj() { ErrorMessage = e.Message }));
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
这样做是可以的,但是当我尝试使用
HttpContext.Current.Response.Write(serializer.Serialize(system));
时,出现了以下错误:
我想要的是一个代表完整as_system对象的JSON对象,这样我就不必手动映射每个属性了。有什么办法可以解决这个问题吗?谢谢!在序列化类型'System.Data.Metadata.Edm.AssociationType'的对象时检测到循环引用。