lazytom's ASP.NET/C# stuff

 

RemoteInclude

#include for remote pages (ASP.NET)

This is actually pretty easy to do - all that is needed is a ASP.NET UserControl with the following code (this is C#):

using System;
using System.Web;
using System.Web.UI;
using System.Net;
using System.IO;
using System.Web.Caching;

namespace Lazytom
{
  public class RemoteInclude : UserControl
  {
    public string URL = null;
    public int CacheTimeout = 0; 

    protected override void Render ( HtmlTextWriter writer )
    {
      if( URL != null )
      {
        string content = (string)Cache.Get( URL );
        if( content == null )
        {
          HttpWebRequest req = 
            (HttpWebRequest)WebRequest.Create( new Uri( URL ) );

          HttpWebResponse res = (HttpWebResponse)req.GetResponse();
          StreamReader rdr = new StreamReader(res.GetResponseStream());
          content = rdr.ReadToEnd();
          rdr.Close();

          Cache.Insert( URL, content, null, 
            DateTime.Now.AddMinutes( CacheTimeout ), TimeSpan.Zero );
        }

        writer.WriteLine( content );
      }
    }
  }
}

Put this in a .cs file in your web project and compile it. Then in the ASP.NET WebForm where you want to include this, do the following:

1) Add this directive at the top:

<%@ Register TagPrefix="Lazytom" Namespace="Lazytom" Assembly="Lazytom"%>

2) Add this "tag" whereever you want your "remote include" to occur:

<Lazytom:RemoteInclude id="remoteinclude" runat="server" URL="http://yoursite.com/yourpage.html" CacheTimeout="60" />

URL should be the URL of the page you want to include and CacheTimeout sets the number of minutes the page should be kept in the cache (so it doesn't get called/retrieved each time somebody accesses your page). You can set this to 0 if you don't want it to be cached.

Links

lazytom's site
lazytom's blog

lazytom @ Flickr
lazytom @ Bloglines
lazytom's FeedJumbler

Marchon Family
Californiabits

© 2000-2005 by Marcel Marchon - created and designed by lazytom.

Valid XHTML 1.0! Valid CSS!