Get page type from url in C #

Given the url, I should know that the page type is at that url. For example, let's say I have a couple of 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 the method as follows with the following results:

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

Is it possible? How can i do this?

+3
source share
3 answers

From this answer , you can get the class of a specific page. Then you can use reflection to determine its base type. (Note: I have not tried to do this, this is just a suggestion.)

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

How about this?

public Type GetPageTypeByURL(string url)
{
    object page = BuildManager.CreateInstanceFromVirtualPath(url, typeof(object));
    return page.GetType().BaseType.BaseType;
}

Application:

Type pageType = GetPageTypeByURL("~/default.aspx");
+1
source

Just a thought: I assume that you are calling the page from some other program. Get HTML, find the distinctive HTML / hidden element that tells you what the page is. If you are on a server, just load the page as a text file and read it.

0
source

All Articles