In this article I will explain how to use JavaScriptSerializer in Windows Forms, Console, Class Library and Windows Service Applications in C# and VB.Net when the following error occurs.
The type or namespace name 'JavaScriptSerializer' could not be found (are you missing a using directive or an assembly reference?)
Problem
When one tries to use JavaScriptSerializer in Windows Forms, Console, Class Library and Windows Service Applications in C# or VB.Net, the following compilation error is thrown and also the System.Web namespace is also unavailable.
The type or namespace name 'JavaScriptSerializer' could not be found (are you missing a using directive or an assembly reference?)
Cause
JavaScriptSerializer class belongs to the System.Web.Script.Serialization which belongs to the System.Web.Extensions library and by default the System.Web.Extensions assembly is not referenced in Windows Forms, Console, Class Library and Windows Service Applications.
Solution
The solution is fairly simple i.e. to add reference of the System.Web.Extensions assembly to the project in the following way.
1. Right click on the project and click Properties option from the Context Menu.
Then check if the current Framework is Client Profile if yes then change to Normal one.
For example, if the current Framework is .Net Framework 4 Client Profile then change it to .Net Framework 4.
2. Right click on the project and click Add Reference option from the Context Menu.
Then from the Add Reference dialog box, click on .Net Tab and look for System.Web.Extensions assembly. Once you find it simply select and click OK.
3. Once the reference is added, it will be shown in the References folder of the Solution Explorer.
4. Now you can add the following namespace to your code and start using the JavaScriptSerializer class.
C#
using System.Web.Script.Serialization;
VB.Net
Imports System.Web.Script.Serialization