How to include an external html file in asp.net page

22,388

Solution 1

Server side includes are available across different platforms and a useful for including static content. You can use them in IIS but I they need to be enabled in IIS7.

<!-- #include file="Static/Menu.html" -->

Instructions to enable SSI in IIS7 are available at http://tech.mikeal.com/

For dynamic content, there is a built-in method of templating called MasterPages, this should be used instead.

Solution 2

I'm presuming this is web forms?

The most typical way to do this in asp.net would be to use a Master Page. Include your header in the master page and have the content page inherit from this.

Solution 3

In addition to using MasterPage and UserControl, one way that provided here is that using Response.writeFile but The fundamental problem of this solution is that:

ASP will NOT process server-side script in the file. This is because all the ASP code has already run when it includes the file and the server will not go back to read anything for server-side processing again

Solution 4

ASP.NET is a different framework to PHP and therefore you are better-off not trying to replicate the way you might have done things in PHP. Instead of using "include" files for common elements, such as a header, we use Master Pages in ASP.NET. Please see http://www.asp.net/master-pages/tutorials or MSDN on Master Pages. The content in a master page (like any other .aspx page) can be static HTML or dynamically generated mark-up from controls.

Another way to have re-usable blocks of content is to create what are called User Controls. These are basically page-fragments that can be reused like other server controls. You can, for instance, create a menu user-control and then embed that in a common master-page. This is a good way of breaking-down your site into manageable "chunks".

For more information see the question ASP.NET equivalent of server side includes elsewhere on SO.

Share:
22,388
Able Alias
Author by

Able Alias

Updated on July 01, 2020

Comments

  • Able Alias
    Able Alias almost 4 years

    How I can include an external html file in asp.net web page. In PHP we can use include function, if asp.net have any similar function, please let me know. My intention is, i have a common header html file, so I need to include that file in all aspx pages.