4

according facebook's Javascript SDK, facebook application should include "Channel File" on their init code. as published here: http://developers.facebook.com/docs/reference/javascript/

i didnt fully understand why they need that, or what should be the content of that file, but im just using their basic example as it may help in some specific issues.

my question is - they said that this 'channel.html' file should be cached. and even gave an example of how to cache it with PHP:

 <?php
 $cache_expire = 60*60*24*365;
 header("Pragma: public");
 header("Cache-Control: max-age=".$cache_expire);
 header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
 ?>

the thing is, AFAIK, that can't be done with asp.net since, i cannot put c# code in html file.

so in the meanwhile i just add it hard-coded, like:

<head>
    <meta http-equiv="cache-control" content="max-age=31536000;public" />
    <meta http-equiv="expires" content="31536000" />  
</head>

im not sure that this is the right way to do it, since "expires" should be given in a specific format date.

any ideas how can i do it right? maybe i can serve facebook 'channel.aspx' instead?

Yaniv
  • 1,906
  • 2
  • 16
  • 23

2 Answers2

5

Here is how you do it in asp.net per this link:

  1. Create a Generic HTTP Handler (ashx file):

    public class FacebookChannel : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
    
            HttpResponse response = context.Response;
            response.ClearHeaders();
    
            const int cacheExpires = 60 * 60 * 24 * 365;
            response.AppendHeader("Pragma", "public");
            response.AppendHeader("Cache-Control", "max-age=" + cacheExpires);
            response.AppendHeader("Expires", DateTime.Now.ToUniversalTime().AddSeconds(cacheExpires).ToString("r"));
            context.Response.ContentType = "text/html";
            context.Response.Write("<script src=\"//connect.facebook.net/en_US/all.js\"></script>");        
        }
        public bool IsReusable { get { return false; } }
    }
    

Make sure you reference the URL in your FB.init function:

channelUrl : '//WWW.YOUR_DOMAIN.COM/fbchannel.ashx',

Gaff
  • 5,507
  • 3
  • 38
  • 49
  • why using Generic Handler and not an ASPX page? – Yaniv Apr 18 '13 at 13:37
  • 1
    @Yaniv there is less overhead when you use a generic handler. – Gaff Apr 18 '13 at 13:49
  • i'll take your answer with 2 little changes: First, it is better to use ashx, as the it is much smaller header. 1. should be "max-age" and not "maxage". 2. Expires field - why using UniversalTime? some how you are loosing 3 hours that way. – Yaniv Jun 04 '13 at 14:20
  • @Yaniv That code is a direct copy and paste from the link I referenced, although you are correct about the max-age part of the header. As for the universal time, I don't see a need to change it. The example above works fine for my Facebook app. – Gaff Jun 04 '13 at 14:57
  • Regarding differernce between aspx and ashx, i found a very good explanation here - http://stackoverflow.com/questions/5469491/aspx-vs-ashx-main-difference – Rasshme Chawla Sep 19 '13 at 01:14
0

channel file is optional but recommended, channel file is used to address certain cross domain communication issues(from your domain to facebook.com) in some browsers, you can use channel.aspx as long as it returns the content described by the API. Caching the channel file is recommended for web applications, read this to know how asp.net handles caching : http://msdn.microsoft.com/enus/library/xsbfdd8c(v=vs.71).aspx

and read the "channel file" topic in the link you described to know more.

Amanpreet
  • 128
  • 1
  • 10