find ASPX and ASCX pages in a web project..

M

Milsnips

hi there,

Can someone help me out with some example code (VB.NET) here that i need:

i would like to programatically check my ASP.NET web application and return
a list of all the ASPX and ASCX pages in the project, then return them in an
string array, eg. name : type ("default.aspx : ASPX")

i figured if i use the io.directory, it might even return items which could
be excluded from the project, which i dont want, thats why i think it might
be best to use the assembly??

thanks,

Paul.
 
?

=?iso-8859-1?Q?Anders_Nor=E5s?=

You can come pretty close by using reflection.
The following snippet creates an ArrayList with the class
name and type of all pages and user controls within the
assembly:
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.FullName,type.BaseType));
}
}

You will need to add the following using statement to the
class "using System.Reflection;"

Usually you give the code behind class the same name as
the aspx or ascx page. Eg. WebForm1.aspx has a class
named WebForm1. If you follow this patter you can
append ".aspx" or ".ascx" to the class names to get the
filename of the page or control.

Regards,
Anders Norås - blog:
http://dotnetjunkies.com/weblog/anoras
 
M

Matt Berther

Hello Anders,

One step is missing from your otherwise great instructions:

You're assuming that there is only one assembly containing the AS?X files
and that it is the currently executing one. It is very probable that a user
may have multiple assemblies in the bin directory (this is a practice I do
frequently).

To get around that, you'll need to add a loop to iterate the assemblies:

foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
// your code goes here
}

After that, you'll want to modify your allTypes assignment this way:

Type[] allTypes = asm.GetTypes();
 
M

Milsnips

great!! thanks for the excellent example.. i had a fair idea what i was
after, but just didnt know exactly where to start code-wise.

just one small issue, i've converted the code into VB.NET, but one line is
giving me a problem:

If t.IsSubclassOf(typeOf(System.Web.UI.Page)) Or
t.IsSubclassOf(typeof(System.Web.UI.UserControl)) Then


it doesnt like this line of code how i have written it,any help?

thanks,
Paul

Matt Berther said:
Hello Anders,

One step is missing from your otherwise great instructions:

You're assuming that there is only one assembly containing the AS?X files
and that it is the currently executing one. It is very probable that a user
may have multiple assemblies in the bin directory (this is a practice I do
frequently).

To get around that, you'll need to add a loop to iterate the assemblies:

foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
// your code goes here
}

After that, you'll want to modify your allTypes assignment this way:

Type[] allTypes = asm.GetTypes();

--
Matt Berther
http://www.mattberther.com
You can come pretty close by using reflection.The following snippet
creates an ArrayList with the class name and type of all pages and
user controls within the assembly: 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.FullName,type.BaseType)); } }You will need to add
the following using statement to the class "using System.Reflection;"
Usually you give the code behind class the same name as the aspx or
ascx page. Eg. WebForm1.aspx has a class named WebForm1. If you
follow this patter you can append ".aspx" or ".ascx" to the class
names to get the filename of the page or control.Regards,Anders
Norås - blog: http://dotnetjunkies.com/weblog/anoras>-----Original
Message----->hi there,>>Can someone help me out with some example
code (VB.NET) here that i need:>>i would like to programatically
check my ASP.NET web application and return>a list of all the ASPX
and ASCX pages in the project, then return them in an>string array,
eg. name : type ("default.aspx : ASPX")>>i figured if i use the
io.directory, it might even return items which could>be excluded
from the project, which i dont want, thats why i think it might>be
best to use the assembly??>>thanks,>>Paul.>>>.>
 
A

Anders Norås

I'm think the VB.NET equivalent to typeof is GetType but I'm not sure.
BTW: I've got an updated example on how to do this posted on my blog.

Regards,
Anders Norås
blog: http://dotnetjunkies.com/weblog/anoras

Milsnips said:
great!! thanks for the excellent example.. i had a fair idea what i was
after, but just didnt know exactly where to start code-wise.

just one small issue, i've converted the code into VB.NET, but one line is
giving me a problem:

If t.IsSubclassOf(typeOf(System.Web.UI.Page)) Or
t.IsSubclassOf(typeof(System.Web.UI.UserControl)) Then


it doesnt like this line of code how i have written it,any help?

thanks,
Paul

Matt Berther said:
Hello Anders,

One step is missing from your otherwise great instructions:

You're assuming that there is only one assembly containing the AS?X files
and that it is the currently executing one. It is very probable that a user
may have multiple assemblies in the bin directory (this is a practice I
do
frequently).

To get around that, you'll need to add a loop to iterate the assemblies:

foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
// your code goes here
}

After that, you'll want to modify your allTypes assignment this way:

Type[] allTypes = asm.GetTypes();

--
Matt Berther
http://www.mattberther.com
You can come pretty close by using reflection.The following snippet
creates an ArrayList with the class name and type of all pages and
user controls within the assembly: 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.FullName,type.BaseType)); } }You will need to add
the following using statement to the class "using System.Reflection;"
Usually you give the code behind class the same name as the aspx or
ascx page. Eg. WebForm1.aspx has a class named WebForm1. If you
follow this patter you can append ".aspx" or ".ascx" to the class
names to get the filename of the page or control.Regards,Anders
Norås - blog: http://dotnetjunkies.com/weblog/anoras>-----Original
Message----->hi there,>>Can someone help me out with some example
code (VB.NET) here that i need:>>i would like to programatically
check my ASP.NET web application and return>a list of all the ASPX
and ASCX pages in the project, then return them in an>string array,
eg. name : type ("default.aspx : ASPX")>>i figured if i use the
io.directory, it might even return items which could>be excluded
from the project, which i dont want, thats why i think it might>be
best to use the assembly??>>thanks,>>Paul.>>>.>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top