Search code examples
c#.neturl-routing

Get Page's Type from URL in C#


Given a URL, I have to be able to know what the page's Type is that resides at that URL. For example, lets say I have a couple pages.

//first.aspx
public partial class FirstPage : System.Web.UI.Page { }

//second.aspx
public partial class SecondPage : MyCustomPageType { }

I would like to be able to call a method in the following manner with the following results:

GetPageTypeByURL("first.aspx");     //"FirstPage"
GetPageTypeByURL("second.aspx");        //"SecondPage"

Is this possible? How can I do this?


Solution

  • From this answer, it appears that you can get the class of a specific page. You may then be able to use reflection to determine its base type. (Note: I haven't attempted this, it's just a suggestion.)

    System.Web.Compilation.BuildManager.GetCompiledType(Me.Request.Url.AbsolutePath)