find ASPX and ASCX pages in a web project..

Discussion in 'ASP .Net' started by Milsnips, Nov 18, 2004.

  1. Milsnips

    Milsnips Guest

    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.
     
    Milsnips, Nov 18, 2004
    #1
    1. Advertising

  2. 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.
    >
    >
    >.
    >
     
    =?iso-8859-1?Q?Anders_Nor=E5s?=, Nov 18, 2004
    #2
    1. Advertising

  3. Milsnips

    Matt Berther Guest

    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.>>>.>
    >
     
    Matt Berther, Nov 18, 2004
    #3
  4. Milsnips

    Milsnips Guest

    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" <> wrote in message
    news:...
    > 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.>>>.>
    > >

    >
    >
     
    Milsnips, Nov 19, 2004
    #4
  5. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    >> 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.>>>.>
    >> >

    >>
    >>

    >
    >
     
    Anders Norås, Nov 19, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia
    Replies:
    1
    Views:
    531
    Curt_C [MVP]
    Jan 6, 2005
  2. Garth17
    Replies:
    3
    Views:
    3,063
    Brock Allen
    Mar 18, 2005
  3. Handle
    Replies:
    2
    Views:
    711
    Handle
    Dec 4, 2007
  4. RadioSignal

    passing variables from .aspx.cs code pages to .aspx pages

    RadioSignal, Sep 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    2,337
    Mirronelli
    Oct 22, 2005
  5. Handle
    Replies:
    2
    Views:
    174
    Handle
    Dec 4, 2007
Loading...

Share This Page