Sunday 15 March 2015

c# - AnonymousType cannot be serialized. Consider marking it with the DataContractAttribute attribute -


I have a web API and I'm just trying to return some of the data to the calling client in XML format.

I keep getting the following error:

  & lt; Exception Message & gt; Type '& lt; & Gt; f__AnonymousType7`9 [System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.Nullable`1 [System.Int32], system Ktring] 'not sorted Can be done Consider marking it with the DataContractAttribute attribute, and mark all its members that you want them to be serialized with the DataMemberAttribute attribute. If it is a collection, consider marking it with the Collection Datactact Attribute. See the Microsoft .NET Framework Document for other supported types. & Lt; / ExceptionMessage & gt;   

Here is the controller code:

  Public class TribuneShowsController: ApiController {Personal read-only TVDataEntities db; Public TribuneShot Controller () {db = New TVData}; Db.configuration.ProxyCreationEnabled = false; } Public IEnumerable GetTribuneShows (String title = null, string style = null, string showTypeDescription = null, string directorName = null, string released = NULL) {var db.TRIB_LKP_ShowTypes.Where query = show (in db.TRIB_Shows from showTypes select v = & gt; v.ShowTypeCode == shows.ShowTypeCode) .DefaultIfEmpty () {new data source = "Tribune" or, shows.Title, EpisodeId = "", EpisodeTitle = "", style = shows.Category, ShowTypes.ShowTypeDescription, show. Director Name, Show Release YEAR, Season Episode = ""}; If (title! = Null) {query = query.Where (s => s.Title.Contains (title)); } If (style! = Null) {query = query.Where (s => s.Genre.Contains (genre)); } If (showTypeDescription! = Null) {query = query.Where (S = & gt; S. Sotaip details. Incorporated (Sotaip description);} If (directorName! = Null) {query = query.Where (s = & gt; s.DirectorName.Contains (directorName)) ;;} if (release year! = nULL) contains {query = query. where (S = & gt; s.ReleaseYear.ToString (). it (Rilijhiyr)); } return query. Tulist ();} protected override void Dispojh (bool Displeng) {db.Dispose (); base.Dispose (settlement);}}   

This is my first question, I How can I define XML by return item (via API)? When a link goes on, it will get the XML.

The second question is: How do I return the unknown type in the code as XML in my code above the client?

After the text "itemprop =" text ">

Name the type that is now anonymous:

  [DataContract] public class {public string Show Datasource {get} set;} public string title {get} set;} ... etc}   

then

  new show {data source = "Tri Bune ", title = shows.Title, EpisodeId =" ", epistityle =" ", generator = show. Category, ShotipDiscription = Show Type Show Show, Director Name = Show. Director Name, Release Year = Show. Release Year, Season Episode = ""};    

No comments:

Post a Comment