8

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

Aishwarya Shiva
  • 3,460
  • 15
  • 58
  • 107
  • What you mean since data loaded from JavaScript is not visible to the search engine crawlers ? did you try to update some partial page or full of page ? – viyancs Apr 21 '14 at 08:40
  • Ya m trying to load the content into a div from JavaScript and I also wanted it to be visible to search engines that's why m also loading it from code behind. – Aishwarya Shiva Apr 21 '14 at 11:20
  • 2
    Since you're calling it from C# on the server side, why are you bothering at all with a generic handler? Why not just call a method directly? – mason Apr 21 '14 at 17:02

3 Answers3

6

A nice way of doing it is to use Routing. In the Global.asax

protected void Application_Start(object sender, EventArgs e)
{
  RegisterRoutes(RouteTable.Routes);
}        

private void RegisterRoutes(RouteCollection routes)
{
  routes.MapHttpHandlerRoute("MyRouteName", "Something/GetData/{par1}/{par2}/data.json", "~/MyHandler.ashx");
}

This is telling ASP.Net to call your handler on /Something/GetData/XXX/YYY/data.json.

You can can access Route Parameters in the handler: context.Request.RequestContext.RouteData.Values["par1"].

The crawler will parse URLs as long as they are referenced somewhere (i.e. robots file or links)

Stefano Altieri
  • 4,550
  • 1
  • 24
  • 41
2

Your Problem is

  1. Load Content into Div using Javascript in ASP.NET using C#.
  2. Visible to Search Engines

My Opinion

  1. When You want Update Partial Page there are some handler or service to communicate between server and client You can Using Ajax for Request to server.

if you use jquery you can try this function jQuery.ajax(); example:

                  $.ajax({
                    url:"/webserver.aspx",
                    data:{id:1},
                    type:'POST',
                    success: function(data) {
                              //do it success function
                       }
                  }) ;

Next Step is Generate Web Service in Code behind Your ASP.NET that should be result as JSON or XML format, whatever you use make sure you can parse easily in success function of jQuery.ajax();

Here some Reference for Generate Web Service on ASP.NET

2.Visible to Search Engine actually

I think if You allow Search engine to Index your page it's no problem , Even if You have some Ajax Code , Search engine will be indexing your page.

viyancs
  • 2,289
  • 4
  • 39
  • 71
  • Search engine visibility is not my problem and also "Loading content into div is my problem". My problem is that I want to send JSON data to a handler from code-behind. – Aishwarya Shiva Apr 25 '14 at 20:24
  • I think code behind in server side , if you want to send data JSON in code behind this should be work with call function and params, then your params is JSON type, and to generate that from code behind you can use this tutorial http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx – viyancs Apr 27 '14 at 16:36
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);
        }
Stefano Altieri
  • 4,550
  • 1
  • 24
  • 41
  • I am getting an error at `var requestStream = request.GetRequestStream();` Error: `Cannot send a content-body with this verb-type.` – Aishwarya Shiva Apr 29 '14 at 17:56
  • Solved. Found the solution here: http://stackoverflow.com/questions/3981564/cannot-send-a-content-body-with-this-verb-type and its now working thanks :) – Aishwarya Shiva Apr 29 '14 at 18:37