Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kaidanov/cd026cf3ce1ecd26d98370a7ffe36ce0 to your computer and use it in GitHub Desktop.
Save Kaidanov/cd026cf3ce1ecd26d98370a7ffe36ce0 to your computer and use it in GitHub Desktop.
Removing backslash from JSON by returing Content
public class Contact
{
public int Id { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
...
//[JsonIgnore]
//[JsonProperty("notificationContact")]
public List<NotificationContact> NotificationContact{get; set;}
}
public class NotificationContact
{
public int Id { get; set; }
public int ContactId { get; set; }
//[JsonIgnore]
public Contact Contact { get; set; }
public int NotificationId { get; set; }
//[JsonProperty("notification")]
public Notification Notification {get;set;}
}
public static class ContactSerialize
{
public static string ToJson(this Contact self) => JsonConvert.SerializeObject(self,Converter.Settings);
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
}
[HttpGet("{id}")]
public async Task<IActionResult> GetContact([FromRoute] int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var contact = await _context.Contact
.Include("NotificationContact.Notification")
.Include("UserContactTypeContact.ContactType")
.Include("Address")
.SingleOrDefaultAsync(m => m.Id == id);
if (contact == null)
{
return NotFound();
}
return Content(contact.ToJson());
//return contact;
}
///Output
{
"id": 3031,
"userName": null,
"firstName": "..",
"lastName": "..",
"email": "..",
"irs": "..",
"portCode": null,
"selfFilerCode": "..",
"achCode": "..",
"picPath": null,
"address": {
"id": 4028,
"street": "77 Turner St Middlesex County",
"addressPart1": null,
"addressPart2": null,
"city": "Woodbridge Township",
"state": "New Jersey",
"zip": "07064",
"country": "United States"
},
"notificationContact": [
{
"id": ..,
"contactId": ..,
"notificationId": 3,
"notification": {
"id": 3,
"description": "SMS"
}
}
],
"userContactTypeContact": [
{
"id": ..,
"userId": 4,
"user": null,
"contactId": 3031,
"contactTypeId": 8,
"contactType": {
"id": 8,
"description": "Chat"
}
},
{
"id": 4105,
"userId": 4,
"user": null,
"contactId": 3031,
"contactTypeId": 7,
"contactType": {
"id": 7,
"description": "Stuffer"
}
}
],
"userId": 4,
"user": null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment