Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  183] [ 5]  / answers: 1 / hits: 5619  / 10 Years ago, fri, april 18, 2014, 12:00:00

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);


More From » c#

 Answers
2

Not sure why you want to do it, but to add a content to an HTTP request use:



HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Common.host + Handlers/SideContentLoader.ashx?typeOfContent=1&UCArgs=cdata);
var requestStream = request.GetRequestStream();
using (var sw = new StreamWriter(requestStream))
{
sw.Write(json);
}

[#45903] Thursday, April 17, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileenreynap

Total Points: 140
Total Questions: 106
Total Answers: 99

Location: Andorra
Member since Sun, Oct 18, 2020
4 Years ago
;