5

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.

John Boker
  • 82,559
  • 17
  • 97
  • 130

5 Answers5

3

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.

morgan_il
  • 1,501
  • 16
  • 19
2

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.

Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
  • This doesn't work because System.Web.HttpContext.Current.Request.Url.AbsolutePath gives me /basketball/home, which doesnt really exist. – John Boker Apr 06 '09 at 13:33
  • I have tried finding it using the routing table, but there's no public member or accessor for the handler. That article on constructing a URL from a route only gives the URL, which would be '/basketball/home' and not the aspx page. – John Boker Apr 06 '09 at 13:45
  • trying one more thing described in that article. – John Boker Apr 06 '09 at 13:46
  • yeah, the method in that article didn't work either, there isn't a way to get to the .aspx page that i can see. – John Boker Apr 06 '09 at 13:52
  • Perhaps using reflection you could get the current class, which would map 1:1 to your aspx file – Chris Ballance Apr 06 '09 at 14:27
1

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.

ejcortes
  • 609
  • 7
  • 13
0

Try using this code:

Page.AppRelativeVirtualPath
Alvin
  • 985
  • 5
  • 13
-1

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.

John Boker
  • 82,559
  • 17
  • 97
  • 130