Include files with asp.net

 

An ASP.NET include, or server-side include, is a plain HTML, HTML/ASP.NET, or ASP.NET-only file that sits outside of a parent Web page. The power of a simple include file is tremendous—it allows you to re-use content that will appear on multiple pages throughout your site. Instead of making the same change 100 times to update 100 pages, you could use an include, update the include file once and have the change populated across all 100 pages. Nice, huh?

When to use ASP.NET includes

Whenever you can. Any piece of content that will appear on multiple pages should be an include, especially on larger sites. As you become familiar with them, you will get creative in their usage. Some Common uses are site navigation, copyright info, footer links, and “News” sections, to name a few.

You should use a small amount of discretion, however, when deciding when and where to use them. If the content is not reused on multiple pages or you don’t have a slick dynamic script that requires them, you don’t need to use includes. Include files put a minor amount of extra strain on the server. When they are used correctly, the benefits far outweigh any performance issues. If you go crazy and start using them for every small bit of code, though, you may notice a difference in your site’s load time. Just something to keep in mind.

Using an ASP.NET include

Let’s assume you have a 100 page website. On the bottom of all 100 pages you have secondary navigation, copyright information, and a link to your privacy policy. If we decide to add an additional link to the footer navigation one day, it would really suck to make the update 100 times by copying and pasting code into every file. Luckily, if we use ASP.NET includes, we only need to make the update once.

Here’s an example of a basic ASP.NET file (we’ll call it index.aspx):


<Script Runat="Server">
'Any ASP.NET code would go here
</Script>
<html>
<head>
<title>My ASP.NET Home Page</title>
</head>
<body>
Some text goes here
</body>
</html>

Here’s our ASP.NET include file to include the footer links and copyright information (we’ll call it inc_footer.aspx):


<p>
Copyright 2004-2005 mysite.com. All rights reserved. <br/>
<a href="index.aspx">Home</a> |
<a href="about-us.aspx">About Us</a> |
<a href="services.aspx">Services</a> |
<a href="contact.aspx">Contact</a> |
<a href="privacy-policy.aspx">Privacy Policy
</p>

To include our copyright and footer links in the home, about, services, contact, and every other page, we only need to add one line of code to each of them:

<!--#include file="inc_footer.aspx"-->

index.aspx now looks like:


<Script Runat="Server">
' Any ASP.NET code would go here
</Script>
<html>
<head>
<title>My ASP.NET Home Page</title>
</head>
<body>
<p>Some text goes here</p>
<!--#include file="inc_footer.aspx"-->
</body>
</html>

That’s it! For every other page in the site, you only need to add one line of code to insert the footer content. A new year rolls around? No problem. Open up inc_footer.aspx, change the year, upload it, and the update is made across every page in your site.

Naming your ASP.NET include files

Technically, you can name your ASP include file whatever you want, as long as it ends in ASPX or INC. I recommend giving your include files a prefix of “inc-” (as in inc-filename.aspx) or “inc_” (as in inc_filename.aspx). You might even consider placing them in their own folder. I find that this helps in managing files, especially when working on a bigger site.

I strongly recommend giving your include files an .ASPX extension only. The reason is this– if someone snooping around in your site guesses the correct filename for your include file and the extension ends in .inc, the full source code will be displayed in the browser (the browser sees a “.inc” file as plain text, not a Web document). This is a huge security flaw, especially when you move into more complex programming (can reveal database connection strings, passwords, etc.). If the snooper guesses correctly but your include file ends in .ASPX, any ASP.NET code will be processed first (the server recognizes it as a server-side programming file), and the person will only see the OUTPUT of the code, not the source code itself.

Leave a comment