简体   繁体   中英

How does ASP.NET MVC Generate JsonResults?

I want to inject some Json into the Html page returned by the server?

Is there a public function that returns the Json string when Json(someObject) gets called?

Note that I don't want to return Json to the browser in a seperate request. I want to convert an object graph to json and inject it into a script block.

Thanks


Looking at the MVC source code, I found this:

using System.Web.Script.Serialization;
...
            // The JavaScriptSerializer type was marked as obsolete prior to .NET Framework 3.5 SP1
#pragma warning disable 0618
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            response.Write(serializer.Serialize(Data));
#pragma warning restore 0618

Why was it marked obsolete?

/*I think */it uses an instance of the System.Web.Script.Serialization.JavaScriptSerializer class.

Verified with reflector that it actually does. It's a default one too, no arguments to the constructor and no properties set before it's used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM