If your site runs on an ASP.NET server and does not support the classic SSI method of including files for output.


<!--#include virtual="/myinclude.asp" -->

Then you may wish to implement your own version of SSI.

In my particular example, I am reading from a HTML file on the server and processing the content before rendering it to the client. I don't see any reason however, why this technique could not be applied in the 'Render' phase of any ASP.NET page.

Essentially, the following function will look for any server side include code in the HTML it is about the render. It will then attempt to process the files that have been included and then swap out the content in the original HTML.

Public Function RunSSI(ByVal htmlContent As String) As String
        'look for SSI
        While htmlContent.Contains("<!--#include virtual=")
                Dim startOfSSI As Integer = htmlContent.IndexOf("<!--#include virtual=")
                Dim LengthOfSSI As Integer = htmlContent.IndexOf("-->", startOfSSI) + "-->".Length - startOfSSI

                Dim ssiText As String = htmlContent.Substring(startOfSSI, LengthOfSSI)
                Dim ssiFile As String = ssiText.Substring(ssiText.IndexOf("virtual=""") + "virtual=""".Length, ssiText.IndexOf("""", ssiText.IndexOf("virtual=""") + "virtual=""".Length) - (ssiText.IndexOf("virtual=""") + "virtual=""".Length))

                'execute the file
                Dim contentResponse As String
                        Dim wc As New Net.WebClient()
                        Dim URL As String = ""
                        If ssiFile.startsWith("/") Then
                                URL = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, ssiFile)
                                URL = ssiFile
                        End If

                        contentResponse = wc.DownloadString(URL)
                Catch ex As Exception
                        contentResponse = "<!--SSI Processing Error: " & ex.Message & " " & """" & URL & """-->"
                End Try
                'now replace the include with the response
                htmlContent = htmlContent.Remove(startOfSSI, LengthOfSSI)
                htmlContent = htmlContent.Insert(startOfSSI, contentResponse)

        End While

        Return htmlContent
End Function

You will notice, that the SSI markup should be the 'virtual' version and that the code will only work with an absolute path (either http://... or /...).

For the avoidance of a permaloop and to help debugging any SSI which is not successfully processed is replaced with a comment stating the error and URL which it attempted to process.