8

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?

6 Answers 6

16

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();
Sign up to request clarification or add additional context in comments.

Comments

4

Instead you might need:

using System.Web.Script.Serialization;

Comments

4

To Solve The Problem -> Just Take reference Step->BY->Step

1.-> Go To Reference of Your project File.

2.-> Right click Then Go To Add Reference

3.-> Go To Framework

4.-> Then Take The Reference--->System.Web.Extensions

5.-> Problem Solved.

Comments

3

I don't know if you just incorrectly typed the line, but it's JavaScriptSerializer, not JavascriptSerializer (Capital S in Script).

4 Comments

Thank you so much. I keep forgetting that C# is case sensitive (been stuck in VB for several years now)...
Ha! How on earth did you compile it? I guess you didn't as you say you're not using VS, just out of interest, what are you using?
I don't really know how to use Visual Studio the "right" way. I just use it to edit code, and I run the web site on a local IIS implementation (so I don't use the Build/Rebuild/Debug tools). So, I guess IIS compiles it, right...?
Wow, sounds like you're flying by the seat of your pants! You might want to look into VS Express and the publish features of a project. You could save yourself design time headaches such as this by compiling your website in a dev environment. Good luck!
0

so I know the requisite DLLs are present and correctly configured.

They may be bin deployed to that project. Have you tried setting CopyLocal to true in the properties of the reference in VS and redeploying?

Comments

0

I had this exact same problem for an older ASP.NET Framework site, to which I had recently updated the VS "Solution" (.sln) file to Visual Studio 2022. Per the answers above, I tried adding the System.Web.Extensions assembly tag in my Web.Config file, both manually and using the VS References dialog, WITHOUT success.

After more searching and playing around with things, the fix ended up being a change to the "Target Framework" setting from 4.8.1 to 4.8 and then back to 4.8.1 in the "Property Pages" for the site. Making this change added the following compilation tag (with the targetFramework attribute set to 4.8.1) to my Web.Config file:

<system.web>
  ...
  <compilation targetFramework="4.8.1"/>
</system.web>

And when I did this, I did NOT need the reference to the System.Web.Extensions assembly in my Web.Config file. Just adding the <compilation targetFramework="4.8.1"/> tag alone fixed the compile errors for the "using" statements to the System.Web.Script.Serialization namespace and the compile errors for my JavaScriptSerializer variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.