What is the correct way to include code from an external file in ASP.NET?

I've been using PHP to build websites for many years. Often I use include for input, for example navigation menu:

<?php include 'includes/nav.php'; ?>


I am a lot more getting started when it comes to ASP.NET (C #). I'm wondering what's the correct (and most efficient) way to make the PHP equivalent included in ASP.NET?


source to share

6 answers

Probably the most similar ASP.NET construct to PHP involves, for your example, creating and referring to a custom control . This allows you to predefine the markup as well as any server side functionality in an ASCX file that you can use on your page.

You can also use the master page like below, which removes your main layout and allows you to define content holders that other pages can implement and fill content. Master Pages is a popular approach for defining page elements that are consistent across all pages, such as your navbar (also like headers, footers, general scripts, CSS, etc.).



One way to accomplish this is using the Main Pages

You define layouts and pinpoints on these layouts.

Another way is User Controls . You put the markup in a separate file (* .ascx) and then link to it in pages or master pages (this is more like include).

I advise you to read the book about ASP.Net in the most polite manner.



It depends on the role played by the include file. Reusable content bits can be created as custom controls or can be part of the master page.

Take a look at this article: http://www.mikesdotnetting.com/Article/144/Classic-ASP-Include-Files-in-ASP.NET



If you want to link and use the control.

<%@ Register Src="includes/Nav.ascx" TagPrefix="uc" TagName="Nav" %>


Then you can add the control to your page.

<uc:Nav ID="navMenu" runat="server" />


If you want to refer to the namespace to be used in the code block <% code here %>

<%@ Import Namespace="Includes.Nav" %>




You don't really include code in your text this way. ASP.NET works with classes and assemblies. Depending on how you are building your asp and what you are trying to do. You can add link to dll or remove class in special classes folder. If you are trying to share the layout the regular page is used.



Disclaimer: I have no experience with PHP, and am making some assumptions based on what I know.

A regular C # file (including ASP.NET Web Forms code behind the file or ASP.NET MVC controller) uses a using statement that imports the namespace. It looks like this:

using ParentNamespace.ChildNamespace;


This allows the classes in ChildNamespace to be used without entering the fully qualified class name ParentNamespace.ChildNamespace.SomeClass. Keep in mind that before doing this, you must add an assembly reference (usually a DLL file) to your project references (managed through the reference types folder in Visual Studio). One assembly can have multiple namespaces and classes, and the standard project templates add some assemblies that are added by default by default.

In an ASP.NET Web Forms project, you can reference what is called a custom control . User controls are encapsulated view + code similar to your page, which are very easy to reuse. You reference the control at the top of the page (or control) like this:

<%@ Register src="~/Folder/Subfolder/MyControl.ascx" tagname="MyCustomControlName" tagprefix="myCustomPrefix" %>


and then wherever you want your control, you use it like this:

<myCustomPrefix:MyCustomControlName ID="myControl" runat="server" />


In ASP.NET MVC, you can refer to namespaces using in code blocks (the syntax depends on the view engine you are using, but this is something using) and you can reuse other views by calling the Html.RenderPartial method like this



Another way to get a link back to a page is with Master Pages . They allow you to define owners of places for your page content.



All Articles