Different ways of doing serialization and deserialization in .Net (Asp.net/Asp.net MVC) (Part 3)


Method 1: Using System.Runtime.Serialization.Json Namespace

Method 2: Using System.Web.Script.Serialization Namespace

Method 3:

In this method we will use the Newtonsoft.Json dll

This is one of the most popular open source dll, we install it from Nuget package manager or from google download it and add to our project

We can write code as given below


using Newtonsoft.Json;
using System;

namespace WebApplication1
{
    public partial class Demo3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EmpDemo objEmp = new EmpDemo
            {
                Id = 1021,
                EmpName = "Chandradev",
                EmpAddress = "Bangalore"
            };

            string jsonData = JsonConvert.SerializeObject(objEmp);
            Response.Write("<b>Converting to Json </b> " + "</br>");
            Response.Write("</br>");
            Response.Write(jsonData);
            Response.Write("</br>");

            var objEmp1 = JsonConvert.DeserializeObject<EmpDemo>(jsonData);
            Response.Write("</br>");
            Response.Write("Converting Json to .Net Object:");
            Response.Write("</br>");
            Response.Write("Id: " + objEmp1.Id + "</br>");
            Response.Write("EmpName: " + objEmp1.EmpName + "</br>");
            Response.Write("EmpAddress: " + objEmp1.EmpAddress + "</br>");
        }

        public class EmpDemo
        {
            public int Id { get; set; }
            public string EmpName { get; set; }
            public string EmpAddress { get; set; }
        }
    }
}

Note: donot forget to include the Newtonsoft.Json namespace in your code. This approach can be used in Asp.net or asp.net mvc application.

Different ways of doing serialization and deserialization in .Net (Asp.net/Asp.net MVC)(Part 2)


different-ways-of-doing-serialization-and-deserialization-in-net-asp-netasp-net-mvc (Part 1)

Method 2 approach

we will use System.Web.Script.Serialization namespace of .net framework

Step 1: Create the EmpDemo Class and write the code in code behind file like given below


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

namespace WebApplication1
{
    public partial class Demo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EmpDemo objEmp = new EmpDemo
            {
                Id = 1021,
                EmpName = "Chandradev",
                EmpAddress = "Bangalore"
            };

            JavaScriptSerializer js = new JavaScriptSerializer();

            Response.Write("<b>Converting to Json </b> " + "</br>");
            Response.Write("</br>");
            string jsonData = js.Serialize(objEmp);
            Response.Write(jsonData);
            Response.Write("</br>");

            var objEmp1 = js.Deserialize(jsonData);
            Response.Write("</br>");
            Response.Write("<b> Converting Json to .Net Object: </b>");
            Response.Write("</br>");
            Response.Write("Id: " + objEmp1.Id + "</br>");
            Response.Write("EmpName: " + objEmp1.EmpName + "</br>");
            Response.Write("EmpAddress: " + objEmp1.EmpAddress + "</br>");
        }
    }

    public class EmpDemo
    {
        public int Id { get; set; }
        public string EmpName { get; set; }
        public string EmpAddress { get; set; }
    }
}

Summary: This approach will be suitable in asp.net or asp.net mvc application.

Different ways of doing serialization and deserialization in .Net (Asp.net/Asp.net MVC)(Part1)


Nowadays so many times we will get requirement to do serialization and deserialization process in .net application.
Firstly we will know what this process is,

Serialization: It is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.

Deserialization: It is the reverse process of Serialization.

In asp.net we can achieve it using so many ways

1. Using System.Runtime.Serialization.Json Namespace

2. Using System.Web.Script.Serialization; Namespace

3. Using Newtonsoft.Json; Open Source dll

Method 1 : using System.Runtime.Serialization.Json Namespace

In approach we will firstly create the C# object then convert into Json object and revert back to its original state

Step1: Create the C# Class like this and use System.Runtime.Serialization namespace as given below

using System.Runtime.Serialization;

namespace WebApplication1
{
    [DataContract]
    public class Emp
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string EmpName { get; set; }

        [DataMember]
        public string EmpAddress { get; set; }
    }
}

Step 2:

write the code in code behind file of asp.net as given below

using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace WebApplication1
{
    public partial class Demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnMethod1_Click(object sender, EventArgs e)
        {
            //Serilization process using  System.Runtime.Serialization

            Emp objEmp = new Emp()
            {
                Id = 1021,
                EmpName = "Chandradev",
                EmpAddress = "Bangalore"
            };

            DataContractJsonSerializer objJS = new DataContractJsonSerializer(typeof(Emp));
            string json = string.Empty;
            using (MemoryStream objMS = new MemoryStream())
            {
                objJS.WriteObject(objMS, objEmp);
                objMS.Position = 0;
                using (StreamReader sr = new StreamReader(objMS))
                {
                    json = sr.ReadToEnd();
                    Response.Write("<b>Converting to Json </b> " + "</br>");
                    Response.Write("</br>");
                    Response.Write(json);
                    sr.Close();
                    objMS.Close();
                }
            }

            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                Emp objEmp1 = (Emp)objJS.ReadObject(ms);
                Response.Write("</br>");
                Response.Write("</br>");
                Response.Write("<b>Converting Json to .Net Object</b>");
                Response.Write("</br>");
                Response.Write("</br>");
                Response.Write("Id: " + objEmp1.Id + "</br>");
                Response.Write("EmpName: " + objEmp1.EmpName + "</br>");
                Response.Write("EmpAddress: " + objEmp1.EmpAddress + "</br>");
            }
        }
    }
}

Step 3: Now run the application you will see the process of serialization and deserialization.

Summary: This approach will be more suitable in WCF application.

HTTP Error 403.14 – Forbidden The Web server is configured to not list the contents


While hosting the asp.net or asp.net MVC application on IIS, you might have faced this exception on browser
“HTTP Error 403.14 – Forbidden The Web server is configured to not list the contents”

To fix this issue, you might have to check the two possible setting in IIS

First: Make sure that the .NET Framework version value is v4.0.

iis_error1

For this you have to do like this

1. Open the Run dialog, type inetmgr and then click OK. This opens the IIS Manager.

2. In the left tree-view, locate the Sites node and find the Default Web Site node under it (or the name of the site where the error message appears).

3 Right-click the node and select Manage web site -> Advanced settings…. Note the name of the value Application pool. Close this dialog.

4. In the treeview to the left, locate and select the node Application pools.

5. In the list to the right, locate the Application pool with the same name as the one you noted in the web site settings. Right-click it and select Advanced settings

6. Make sure that the .NET Framework version value is v4.0. Click OK.


Second: Asp.net is not registered on Server

For this you have to go to
>>Visual Studio
>> Then Visual Studio Tool >> Visual Studio Command Prompt

>> Then Go to the given path C:\Windows\Microsoft.NET\Framework64\v4.0.30319
>> Then run the aspnet_regiis.exe -ir

iis_error