0

If I return an object containing a DateTime property from a web method, which would be methods prefix with the data annotation

[WebMethod]

that date will be serialized to something like

"/Date(1365776562553)/"

instead of a string in iso-8601 format like

"2013-04-12T19:59:53.2422758+05:30"

If I could change the serializer for web methods from JsonDataContractSerializer to NewtonSoft Json Serializer, the dates would be fixed.

How?

I will not serialize manually or writing javascripts that consumes date strings in the first format.

The googling I have done this far only directs me to pages where any of those workarounds are suggested.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Anders Lindén
  • 6,839
  • 11
  • 56
  • 109
  • `WebMethod` is an attribute used for the old ASMX *SOAP* attributes that were replaced by WCF back in 2005. It's not a Data Annotation attribute. Those services can't return JSON anyway. `JsonDataContractSerializer` is a WCF class - DataContractSerializer is the serializer used by WCF. What stack are you really using? WCF Rest perhaps? – Panagiotis Kanavos Nov 27 '18 at 10:34
  • 1
    You can add a string property to the class like `date_iso` and fill it with the correct date string format. – VDWWD Nov 27 '18 at 10:50
  • Or make the return type a void and write the json string to the context. – VDWWD Nov 27 '18 at 11:19
  • Writing json string to the context will not only do the serialization manually (like I did not want to do) but also add the badness of returning void where you conceptually are returning something non void. – Anders Lindén Nov 27 '18 at 11:46
  • Making the property a string will throw away the type which adds badness. – Anders Lindén Nov 27 '18 at 11:46
  • @AndersLindén are you using WCF Rest? 4.0? 4.5? Why use WCF Rest at all? There are duplicate questions but the solution is *not* that easy - you can either [create a TextResponse containing the JSON string](https://stackoverflow.com/questions/3118504/how-to-set-json-net-as-the-default-serializer-for-wcf-rest-service) in every method or [create a custom message formatter](https://stackoverflow.com/questions/11003016/c-sharp-wcf-rest-how-do-you-use-json-net-serializer-instead-of-the-default-dat). Given the stack's age you'll see that duplicate SO questions dry up after 2012 and Web API – Panagiotis Kanavos Nov 27 '18 at 13:11
  • Possible duplicate of [How to set Json.Net as the default serializer for WCF REST service](https://stackoverflow.com/questions/3118504/how-to-set-json-net-as-the-default-serializer-for-wcf-rest-service) – Panagiotis Kanavos Nov 27 '18 at 13:13
  • @PanagiotisKanavos I am using .NET Framework 4.5. Is that an answer? I really want to return the object I am using itself, not to wrap it in a TextResponse like object or in a string. I really want to solve this by configuration. I agree about the linked to duplicate candidate really being a duplicate, but the accepted answer suggests using a generic return type and to manually serialize. – Anders Lindén Nov 27 '18 at 13:23
  • @AndersLindén I posted *two* links. Once shows how to format the string in each action, the other how to do it globally using a custom message formatter. *Why* are you using such old technology though? WCF Rest was never more than a stop-gap measure on the road to MVC and Web API – Panagiotis Kanavos Nov 27 '18 at 13:25
  • We have an old project using webforms. We actually have some web api controllers in that project, which I could use. The point with using WebMethods is that you can stick them to code behinds which makes them less dependent of other functionality. An aspx file could then be self providing with webmethods. – Anders Lindén Nov 27 '18 at 13:28
  • @AndersLindén are you talking about *AJAX* webmethods? Please explain what you're doing and use the correct tags! Even people who *have* used these things can't be expected to remember 10 year-old stacks – Panagiotis Kanavos Nov 27 '18 at 13:37
  • @AndersLindén and that's a problem - that technology is so old it's hard to find any info about formatting right now. – Panagiotis Kanavos Nov 27 '18 at 13:44
  • I agree with you about that. – Anders Lindén Nov 27 '18 at 13:47
  • Maybe this question will not be useful to anyone else so I could close it. webforms is not that bad if you complement it with web api 2 and angularjs (even if it is old). – Anders Lindén Nov 27 '18 at 13:49
  • I still use this tech. Short answer is that you cannot return the object itself. You will have to manipulate the date at some point. Not exactly sure about the default serializer but I think you should be able to use the .Net JavascriptSerializer in code behind. I don't remember what that does to the date off-hand, you might still have to update it. – wazz Nov 28 '18 at 15:45

0 Answers0