ASP.NET SSI Server-Side Includes

ASP.NET web programming framework

SSI directives can be used on IIS. They can include static resources in your ASP.NET website project. We explore details of server-side-include use and processing in ASP.NET pages.

This article shows how to use SSI directives in ASP.NET web pages.

Introduction

Note

First, SSI is not an ASP.NET technology, but server-side includes are supported on IIS. The following comment, in an ASP.NET aspx file, will tell the server (IIS) to insert the text of the file Style1-min.css where the HTML comment is.

<!-- #include file="Style/Style1-min.css" -->

#include attributes

The above snip shows the "file" attribute. It points directly to the file Style1-min.css that is located in the Style folder at the root of the web project, relative to the page. The other option you can add to the #include is virtual, which uses virtual paths.

Appropriate uses

Programming tip

SSI directives are not great for many situations. Use them only for including static resources into .aspx pages. I decided on SSI as a good solution for including CSS markup into HTML headers. These CSS files needed to be minified, and the #include can include the minified files. In the resulting HTML, a style tag will contain the contents of Style1-min.css. Note that the IIS server, not ASP.NET, handles SSI.

<style type="text/css"><!-- #include file="Style/Style1-min.css" --></style>

SSI is outdated

SSI is essentially a macro that can greatly ease development effort on our part. You can duplicate SSI with a web user control, but that is much bulkier and is likely much slower. Let's examine some differences.

Lines of code SSI: 1
ASP.NET control: 20+

Caching SSI: File system cache on IIS7
ASP.NET control: Memory or output cache

Control tree SSI: Literal controls added
ASP.NET control: Custom controls added

Assemblies SSI: 0
ASP.NET control: 1

Managed code SSI: None
ASP.NET control: All

More resources

I investigated SSI quite a lot before I started this article. Microsoft provides a solid foundation article. I learned that the file system cache on IIS can make SSI extremely fast, and that by bypassing managed code, it is hard to beat performance-wise.

Microsoft Support reference

Include styles

Cascading style sheet (CSS)

Minified stylesheets are basically "compressed" CSS, which is important for performance of a site. I have shown the include before, but the following code shows how you can insert a preprocessed stylesheet into a style tag in HTML (an aspx file).

<%-- Style tags in an ASPX page's head element. --%>
<style type="text/css"><!-- #include file="Style/Style1-min.css" --></style>
<style type="text/css"><!-- #include file="Style/Style2-min.css" --></style>

Summary

Here we saw how you can use SSI to optimize performance on both client and server. The example here results in a simpler control tree for ASP.NET, and a smaller page for browsers that download the page. This enhances modularity. File-caching on IIS alleviates any overhead of SSI.

ASP.NET Tutorials
.NET