ASP.NET C#: JavascriptSerializer could not be found

Octavient picture Octavient · Apr 4, 2013 · Viewed 22.5k times · Source

I'm trying to use the JavascriptSerializer object in ASP.NET v4.0 with C#. I'm not using Visual Studio--this is on a live IIS7 server. I can access this object just fine using VB on this same web server, so I know the requisite DLLs are present and correctly configured.

But when I try to use this object in C#, I get this error: The type or namespace name 'JavascriptSerializer' could not be found

In my class file, I have this:

using System.Web;
using System.Web.Script;
using System.Web.Script.Serialization;

In web.config, I have this:

<assemblies>
    <add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>

In my default.aspx.cs file, I have this:

JavaScriptSerializer obj_serializer = new JavascriptSerializer();

It's this last line of code that is causing the above referenced error.

Help?

Answer

Cameron Tinker picture Cameron Tinker · Apr 4, 2013

You need to add a reference to System.Web.Extensions.dll in your application. System.Web.Extensions is the namespace that contains the JavaScriptSerializer class. Then add a using directive to use the namespace System.Web.Extensions like so:

using System.Web.Extensions;

You can also declare your JavaScriptSerializer like this:

var serializer = new System.Web.Extensions.JavaScriptSerializer();