Asp.Net System.Web.Routing Find actual .aspx Page

2019-04-08 16:25发布

I'm using System.Web.Routing to have some better URL's and have come across a problem. I need to know the actual page that's handling the request.

for example a request comes in as:

/basketball/home

I need to find the page that handles that request, like:

/management/default.aspx

I'm only using the System.Web.Routing and not MVC. I have a handle to the RequestContext that contains some of the route information, but i don't see what i need.

Thanks in advance.

******* UPDATE *******

I was able to use Context.CurrentHandler which give me "ASP.management_default_aspx", not exactly the page but enough to get the page name.

5条回答
虎瘦雄心在
2楼-- · 2019-04-08 16:38

Try using this code:

Page.AppRelativeVirtualPath
查看更多
迷人小祖宗
3楼-- · 2019-04-08 16:39

There is actually another simple way to get the actual page:

String vPath = ((System.Web.Routing.PageRouteHandler)Page.RouteData.RouteHandler).VirtualPath
Do not forget to check Page.RouteData.RouteHandler is not null - while you are getting the page w/o ASP.Net routing but directly.

查看更多
何必那么认真
4楼-- · 2019-04-08 16:45

vhinn terrible's answer worked...

Page.AppRelativeVirtualPath

You just have to remove the initial tilde ("~"), and you're ready to go.

var path = Page.AppRelativeVirtualPath.Replace("~", String.Empty);

I don't know why it was downvoted. Worked for me like a charm.

查看更多
可以哭但决不认输i
5楼-- · 2019-04-08 16:47

Can you not retrieve this from the current HttpContext object?

Perhaps something like this:

public string GetCurrentPageName() 
{ 
    string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath; 
    System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath); 
    string sRet = oInfo.Name; 
    return sRet; 
} 

UPDATE:
Have you tried this article?

How to: Construct a URL from a Route

You should be able to retrieve it back from the Routing table you have constructed.

查看更多
Emotional °昔
6楼-- · 2019-04-08 16:51

I was able to use Context.CurrentHandler which give me "ASP.management_default_aspx", not exactly the page but enough to get the page name.

查看更多
登录 后发表回答