我正在尝试使用大量链接表序列化一大堆信息,以便我可以通过 Web 服务传递它并使用 ajax 调用它。我遇到的问题是有一些一对多的关系,还有一些多对多的关系,因为我是新手,所以我不确定如何在通过我的数据库进行无限循环之后停止 json。
有什么我错过了脚本忽略的东西,还是我应该尝试找到一个完全不同的解决方案?
我需要 PageData 中的所有数据
public class SiteData
{
public SiteData()
{
this.UrlResponse = new List<Response>();
}
public SiteData(string url, string robots, string siteMap, bool googleVerification, bool bingVerification, List<Response> urlResponse)
{
this.DomainUrl = url;
this.Robots = robots;
this.Sitemap = siteMap;
this.GoogleVerification = googleVerification;
this.BingVerification = bingVerification;
this.UrlResponse = urlResponse;
}
public SiteData(string url)
{
// TODO: Complete member initialization
this.DomainUrl = url;
}
[Key]
public int Id { get; set; }
[Required]
public string DomainUrl { get; set; }
public string Robots { get; set; }
public string Sitemap { get; set; }
public bool GoogleVerification { get; set; }
public bool BingVerification { get; set; }
public List<Response> UrlResponse { get; set; }
[ScriptIgnore]
public virtual ICollection<PageData> PageDatas { get; set; }
}
public class PageData
{
[Key]
[Required]
public int Id { get; set; }
[Required]
public string PageUrl { get; set; }
public string Analytics { get; set; }
public bool Paginated { get; set; }
public bool Flash { get; set; }
public bool Iframe { get; set; }
public bool NoIndexFollow { get; set; }
public bool SchemaTag { get; set; }
public virtual ICollection<Platform> Platforms { get; set; }
public virtual ICollection<AltTag> AltTags { get; set; }
public virtual ICollection<Canonical> Canonicals { get; set; }
public virtual ICollection<MetaTitle> MetaTitles { get; set; }
public virtual ICollection<MetaDesc> MetaDescs { get; set; }
public virtual ICollection<BlogLocation> BlogLocations { get; set; }
public virtual ICollection<H1> H1s { get; set; }
public virtual ICollection<H2> H2s { get; set; }
public virtual ICollection<H3> H3s { get; set; }
public virtual ICollection<ViewState> ViewStates { get; set; }
//[ForeignKey("DomainUrl")]
public SiteData DomainUrl { get; set; }
//public virtual ICollection<SiteData> SiteData { get; set; }
}
public class Platform
{
public Platform() { }
[Key]
public int KeyId { get; set; }
public string PlatformExtension { get; set; }
public int ResponseCode { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class AltTag
{
public AltTag() { }
public AltTag(int id, string altTag)
{
this.Id = id;
this.AltTagString = altTag;
}
[Key]
public int KeyId { get; set; }
public string AltTagString { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class Canonical
{
public Canonical() { }
public Canonical(int id, string altTag)
{
this.Id = id;
this.CanonicalString = altTag;
}
[Key]
public int KeyId { get; set; }
public string CanonicalString { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class MetaTitle
{
public MetaTitle() { }
public MetaTitle(int id, string metaTitle)
{
this.Id = id;
this.MetaTitleString = metaTitle;
}
[Key]
public int KeyId { get; set; }
public string MetaTitleString { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class MetaDesc
{
public MetaDesc() { }
public MetaDesc(int id, string metaDesc)
{
this.Id = id;
this.MetaDescString = metaDesc;
}
[Key]
public int KeyId { get; set; }
public string MetaDescString { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class BlogLocation
{
public BlogLocation() { }
public BlogLocation(int id, string blog)
{
this.Id = id;
this.BlogLoc = blog;
}
[Key]
public int KeyId { get; set; }
public string BlogLoc { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class H1
{
public H1() { }
public H1(int id, string h1)
{
this.Id = id;
this.H1String = h1;
}
[Key]
public int KeyId { get; set; }
public string H1String { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class H2
{
public H2() { }
public H2(int id, string h2)
{
this.Id = id;
this.H2String = h2;
}
[Key]
public int KeyId { get; set; }
public string H2String { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class H3
{
public H3() { }
public H3(int id, string h3)
{
this.Id = id;
this.H3String = h3;
}
[Key]
public int KeyId { get; set; }
public string H3String { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
public class ViewState
{
public ViewState()
{
this.Existance = new bool();
}
[Key]
public int KeyId { get; set; }
public bool Existance { get; set; }
public int Size { get; set; }
[ForeignKey("PageData")]
public int Id { get; set; }
[ScriptIgnore]
public virtual PageData PageData { get; set; }
}
实际调用:
[WebMethod]
public string GetPage(string pageId)
{
using (var db = new DataContext())
{
PageData page = db.PageDatas.Find(Int32.Parse(pageId));
string json = null;
JavaScriptSerializer jss = new JavaScriptSerializer();
json = jss.Serialize(page);
return json;
}
}