【发布时间】:2011-12-09 02:32:27
【问题描述】:
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)); 时,我收到以下错误:
序列化类型对象时检测到循环引用 'System.Data.Metadata.Edm.AssociationType
我想要的是一个代表完整 as_system 对象的 json 对象,所以我不必手动映射每个属性。有没有办法解决这个问题?谢谢!
【问题讨论】:
标签: c# javascript json entity-framework