从C#中的URL获取页面类型

问题描述 投票:3回答:4

给定一个URL,我必须能够知道该URL所在的页面类型是什么。例如,假设我有几页。

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

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

我希望能够以下列方式调用方法,结果如下:

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

这可能吗?我怎样才能做到这一点?

c# .net url-routing
4个回答
5
投票

this answer,您似乎可以获得特定页面的类。然后,您可以使用反射来确定其基本类型。 (注意:我没有尝试过,这只是一个建议。)

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

1
投票

那这个呢?

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

用法:

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

0
投票

只是一个想法:我假设你从其他程序调用页面。获取HTML,搜索区分HTML /隐藏元素,告诉您页面是什么。如果您在服务器上,只需将页面作为文本文件加载并阅读。


0
投票
Page page = (Page)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(url, typeof(Page));
CustomerPage page = (CustomerPage) SomeMagicalPageCreater.CreatePage("CustomerPage.aspx");

https://forums.asp.net/t/1315395.aspx这是我发现的。

© www.soinside.com 2019 - 2024. All rights reserved.