c# - ASP.NET JavaScriptSerializer requires HttpResponse? -
it appears system.web.script.serialization.javascriptserializer class tries obtain httpresponse current request, presumably apply appropriate character encoding.
however means when to use class no httpcontext in scope, blows following exception + stack trace:
[httpexception (0x80004005): response not available in context.] system.web.httpcontext.get_response() +8753496 system.web.util.httpencoder.get_current() +39 system.web.httputility.javascriptstringencode(string value, boolean adddoublequotes) +13 system.web.script.serialization.javascriptserializer.serializestring(string input, stringbuilder sb) +31 system.web.script.serialization.javascriptserializer.serializecustomobject(object o, stringbuilder sb, int32 depth, hashtable objectsinuse, serializationformat serializationformat) +240 system.web.script.serialization.javascriptserializer.serializevalueinternal(object o, stringbuilder sb, int32 depth, hashtable objectsinuse, serializationformat serializationformat) +1355 system.web.script.serialization.javascriptserializer.serializevalue(object o, stringbuilder sb, int32 depth, hashtable objectsinuse, serializationformat serializationformat) +194 system.web.script.serialization.javascriptserializer.serialize(object obj, stringbuilder output, serializationformat serializationformat) +26 system.web.script.serialization.javascriptserializer.serialize(object obj, serializationformat serializationformat) +74 system.web.script.serialization.javascriptserializer.serialize(object obj) +6
i cannot rework code in such way guarantee existence of valid httpcontext. ideas on how avoid this? might custom javascriptconverter string type robust solution?
thanks
pascal
afaik javascriptserializer doesn't require httpcontext , works fine in console application:
class program { static void main(string[] args) { string json = new javascriptserializer().serialize(new { bar = "foo" }); console.writeline(json); } }
you try json.net.
Comments
Post a Comment