views:

100

answers:

3

Short of parsing the ASPX page myself, what's the way to determine the class of an ASPX page?

In our projects we use Web Projects in VS2008 (instead of Web Sites kind or projects), that gives us a single DLL for the whole site, which is great.

Now I need to determine programmatically the class of an ASPX site.

I KNOW that the ASPX class has the same name of the aspx file (when created with VS), however, the filename may be renamed in production and hence blowing such code to smitherins.

All our ASPX pages starts with the basic @Page directive:

<%@ Page ... Inherits="_CLASS_NAME_GOES_HERE_" %>

So, short of parsing the directive myself, what other options do I have?

Edited to add:

Because we're using a third party software to manage our portal, it requires certain conditions.

I'm trying to create a custom Attribute to mark the pages as "safe" for the portal, and as such all our links between pages need to be parsed through a function that needs to analyse if the destination page is safe.

Edited to clarify

I see that the question may not be as clear as I thought it was, then let me try to clarify it a bit:

Say we have a relative URL to an specifc page, for instance "~/destination.aspx"

I need to determine the class of that page, based on it's URL.

+2  A: 

you could try this.GetType() in the page codebehind

MasterMax1313
A: 

If you own all of the pages that are "safe", maybe you could derive them all from the same base class that had the info you need. Then you could just typecast to the base. If it fails, then you know it can't be safe.

DancesWithBamboo
+1  A: 

The following code does what I think you're looking for, but it uses a method clearly marked as "not intended to be used directly from your code":

string path = "~/MyPage.Aspx";
IHttpHandler handler = System.Web.UI.PageParser.GetCompiledPageInstance(path, Server.MapPath(path), HttpContext.Current);
Response.Write(handler.GetType().BaseType.FullName);

This outputs:

MyApp.MyPage

You need to use GetType().BaseType because ASP.NET dynamically creates a new class for each .ASPX page, which inherits the codebehind class.

Badaro