1 Replies - 812 Views - Last Post: 27 April 2014 - 05:43 AM Rate Topic: -----

#1 ultrapulse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 23-October 13

Send data to a generic handler that accepts JSON data using C#

Posted 26 April 2014 - 06:20 PM

I have a situation where I am accessing an ASP.NET Generic Handler to load data using JQuery. But since data loaded from Javascript is not visible to the search engine crawlers, I decided to load data from C# and then cache it for JQuery. My handler contains a lot of logic that I don't want to apply again on code behind. Here is my Handler code:

public void ProcessRequest(HttpContext context)
        {
            JavascriptSerializer jsonSerializer = new JavascriptSerializer();
            string jsonString = string.Empty;

            context.Request.InputStream.Position = 0;
            using (var inputStream = new System.IO.StreamReader(context.Request.InputStream))
            {
                jsonString = inputStream.ReadToEnd();
            }

            ContentType contentType = jsonSerializer.Deserialize<ContentType>(jsonString);
            context.Response.ContentType = "text/plain";
            switch (contentType.typeOfContent)
            {
                case 1: context.Response.Write(getUserControlMarkup("SideContent", context, contentType.UCArgs));
                    break;
            }
        }



I can call the function
getUserControlMarkup()
from C# but I will have to apply some URL based conditions while calling it. The
contentType.typeOfContent
is actually based on URL parameters.

If possible to send JSON data to this handler then please tell me how to do that. I am trying to access the handler like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Common.host + "Handlers/SideContentLoader.ashx?typeOfContent=1&UCArgs=cdata");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();



But its giving NullReferenceException in Handler code at line:
ContentType contentType = jsonSerializer.Deserialize<ContentType>(jsonString);


Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Send data to a generic handler that accepts JSON data using C#

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Send data to a generic handler that accepts JSON data using C#

Posted 27 April 2014 - 05:43 AM

On line 12, are you sure that jsonString is not null or empty? If it's not null or empty, are you sure that the JSON data corresponds to your ContentType class? If so, can you post both the JSON data and your ContentType class source code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1