Thursday, November 18, 2004 - Posts

Listing all ASP.NET pages and user controls in an assembly

“Milsnips” asked a question in the dotnet.framework.aspnet newsgroup about how to programatically check his ASP.NET web application and return a list of all the ASPX and ASCX pages in the project. He only wanted those who were part of the assembly and none of the pages or controls that had been excluded from his project.
If he follows the default naming pattern for pages and code-behind classes, eg. WebForm1.aspx inherits from the WebForm1 class. The following code snippet will do the trick:

Type[] allTypes=Assembly.GetExecutingAssembly().GetTypes();
ArrayList pagesAndUserControls=new ArrayList();
foreach (Type type in allTypes) {
            if (type.IsSubclassOf(typeof(System.Web.UI.Page))||type.IsSubclassOf(typeof(System.Web.UI.UserControl))) {
                        pagesAndUserControls.Add(String.Format("{0}.{1}",type.Name,
                                    type.IsSubclassOf(typeof(System.Web.UI.Page))?"aspx":"ascx"));
            }
}

A slightly different version of this post has also been published as an answer in the newsgroup.