ASP.NET C#: JavascriptSerializer could not be found
Asked Answered
G

5

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?

Guinness answered 4/4, 2013 at 17:51 Comment(0)
B
3

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

Beefwood answered 4/4, 2013 at 17:58 Comment(4)
Thank you so much. I keep forgetting that C# is case sensitive (been stuck in VB for several years now)...Guinness
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?Lindsay
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...?Guinness
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!Lindsay
S
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();
Sagunto answered 4/4, 2013 at 17:56 Comment(0)
Z
4

Instead you might need:

using System.Web.Script.Serialization;
Zito answered 13/3, 2018 at 16:43 Comment(0)
P
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.

Perforce answered 19/8, 2019 at 8:21 Comment(0)
B
3

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

Beefwood answered 4/4, 2013 at 17:58 Comment(4)
Thank you so much. I keep forgetting that C# is case sensitive (been stuck in VB for several years now)...Guinness
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?Lindsay
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...?Guinness
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!Lindsay
E
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?

Extremadura answered 4/4, 2013 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.