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?
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.
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.
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.
You must be logged in to post a comment.





November 8th, 2005 at 9:28 am
Very good article.
November 8th, 2005 at 11:59 pm
good article… thanks for your guiding for such beginner like me .
April 25th, 2006 at 4:23 pm
Neat!!!! I used .inc files before but this is a much cleaner approach. Thanks for the Article.
November 9th, 2006 at 9:26 am
Though .inc and .asp files includes are allowed in ASP.net project,
its the question of whether you are going back to the past or
moving ahead with time.
A “.ascx” file is a replacement/better alternative for the above include files.
This file could be dragged droped to an .aspx file anywhere in the layout and
with more advanced functionality of its code behind if required.
There are component called “PlaceHolder” and “Control” Class through
which dynamic file change and position swapping could be achieved. For example:
In .aspx.cs code:
————————————————
Control controlExample = LoadControl(”ascxExample.ascx”);
PlaceHolder1.Controls.Add(controlExample);
————————————————
I would strictly suggest a dot net developer to use .ascx as an includes only
December 16th, 2006 at 12:26 pm
Hey ppl,
UserControls are better solution to replacement the ASP #include.
Alex, good comment !
Cya friends
February 5th, 2007 at 2:19 am
Please tell me. is it possible to include a file bitween tags?
February 9th, 2007 at 9:07 am
Really you should be using .ascx web controls rather than server side includes. It’s much better for maintaining the code as well as performance
March 1st, 2007 at 12:10 am
Dear Friends,
In my project they were using lot of include files. In the compilation it’s showing errors like “unknown server tags” . So how suppress these kind of errors in either compilation/Building a setup.
Example:
MainPage.aspx
.
.
.
ordermenu.aspx
In the above example, ordermenu.aspx gives an error with “unknown server tags”. How to solve this.
March 2nd, 2007 at 9:07 am
You said the browser sees the .inc file as a text file which is true, but the real problem is that ASP.net doesn’t parse the file before sending it to the browser, thus exposing all the code.
April 11th, 2007 at 12:50 pm
Is it possible to get this kind of include functionality by pointing to an include file on a remote server? I’d like to put a shared nav in one place and have all my sites include it for ease of maintenance. I’ve tried both ascx’s and this include method but I get parser errors saying “is not a valid virtual path”. Is there anyway around this?
May 8th, 2007 at 3:06 pm
This article should be updated.
1) It should be listed as “ASP.NET 1.x”, since .NET 2.0 allows for Master Pages, which are far better than include files.
2) As another user commented, a user control can be developed that can provide the same look and feel as a master page with only two lines of code in the aspx source: 1 line to register the user control, and a second to put the control on the page.
May 9th, 2007 at 10:37 am
Its great to use includes with Master Pages as well though. For instance, have the header logo and test in an include file with no background color. Then include that on the master page with a colorful background color, but also include the header on printable pages. This way you only have to update the control, not the Master Page and any other pages.
May 23rd, 2007 at 3:12 pm
Hello
I am new to asp.net.
I have been doing .asp for a lonmg time.
I am having major problems getting my asp.net code to work.
especially the include files.
I read the threads but do not what masterpages are.
response.writefile”c:\includefile.inc”
Is that correct?
Also how can I get the include file to be included in the compile?
The variables are not being seen!?!?!?
Thanks
-roy
May 30th, 2007 at 8:12 am
Thanks for the guide… but, how can I include pages dinamically?, for example: I have a page called ‘module.aspx’ that have the ‘?page’ param… and it recive ‘inc_footer.aspx’ and then it write ”… can i do this?
June 6th, 2007 at 3:27 pm
Great & simple instructions. Thanks for the help. This is the first of 50 articles that made it easy.
June 12th, 2007 at 7:38 am
Can I insert an include in a .aspx page and see the included .ascx (or other extension) in “Design develope time” into de .aspx page?
Sorry for my wrong english.
June 13th, 2007 at 11:29 am
What a flash back! The old method returns….
I have been searching for a way to add my programmed pages onto regular html type files that non programmers keep updated. Was trying to use Frontpage and Web Expressions and ran into the webbot not allowing includes with ASP.NET files. Works great making everything aspx files (with a few ascx’s thrown in for good measures). I will go back later and make all my included files ascx’s to get rid of the redundant tags.
June 24th, 2007 at 6:31 am
I tried the include method, it worked but whatever I have in the file I included are coming at the top of the main page. that is i included page 2 in page 1. In the browser the contents of page 2 come at the top then conents of page 1 appear. Any explanation.
July 5th, 2007 at 9:23 am
I wanted to include file which is on one directory up than my web application, but due to some reason asp.net through me compile time error.
July 21st, 2007 at 4:15 pm
If you are receiving a “HTTP Error 404.3 - Not Found” error it might be because you have not installed all parts of IIS, and it doesn’t understand the .aspx extension.
The first time I installed IIS in Vista, I just checked the tickbox, but it didn’t tick every part of it. You have to expand the + signs and tick each thing manually to get it all.
August 13th, 2007 at 10:57 pm
Does including files into my webpages using ASP.net ‘include’ function adversly affect SEO?
November 12th, 2007 at 6:22 pm
The folks on this page claiming that using ascx user controls results in better performance are mistaken. Please check the IIS processing pipeline. SSI’s inject the relevant code in the file prior to aspx processing. The page is built and cached. To asp.net, it appears to be a single page. This has better performance than the overhead of user controls.
SSI’s are by no means obsolete, and are frequently a much better choice and more flexible than user controls.
February 23rd, 2008 at 1:04 pm
I really need to know how to do an include file in the .aspx code behind page. Many people tell me to use classes, and I do. But there are some things I really need to put in include files in the.aspx or .vb portion of the .net project. Please Help !
March 13th, 2008 at 6:31 am
use Response.WriteFile() method
March 15th, 2008 at 4:08 pm
Salve-
Regarding SEO, using includes files in any server-side language (ASP.NET, ASP, PHP, etc) does not have any impact (positive or negative) on SEO.
The server-side code is processed before the final output (HTML) is returned to the Web browser from the server. So technically, it’s possible to use ASP.NET and still return an “search optimized” URL that could potentially help your search rankings.
The other factors that would help SEO (page code, links into the page, proper titles and tagging, etc) are all things that should be done whether you use ASP.NET or plain HTML for your pages.
April 16th, 2008 at 2:51 am
I need response to Gopal Comments as iam facing the same problem in my application:
————–
If you have any solution for that ,please mail me the solution to my mailid
srinivasarao.devu@gmail.com or post ur comments
The Problem explained below:
—————————
Dear Friends,
In my project they were using lot of include files. In the compilation it’s showing errors like “unknown server tags” . So how suppress these kind of errors in either compilation/Building a setup.
Example:
MainPage.aspx
.
.
.
ordermenu.aspx
In the above example, ordermenu.aspx gives an error with “unknown server tags”. How to solve this.
Dave Says:
April 16th, 2008 at 4:56 am
Please i need solution as early as possible for the below mentioned problem…….
————————-
Dear Friends,
In my project they were using lot of include files. In the compilation it’s showing errors like “unknown server tags” . So how suppress these kind of errors in either compilation/Building a setup.
Example:
MainPage.aspx
.
.
.
ordermenu.aspx
In the above example, ordermenu.aspx gives an error with “unknown server tags”. How to solve this.