Kieran Benton
Kieran Benton

Reputation: 8890

How can I get the relative URL of a page from an instance of the page object itself

Given an instance of a class that derives from System.Web.Page (ie. a normal page instance) how can I get the relative URL required to request that page (without a Request object).

To clarify, I'm instanciating a Page derived class outside of the normal page request pipeline.

e.g.

var p = new MyPage();
// p.Request is not going to be valid

From that how can I get the appropriate URL that would normally be used to request the page?

Upvotes: 0

Views: 1138

Answers (3)

Philip Rieck
Philip Rieck

Reputation: 32568

Short answer - you can't, because you're coming at it backwards. Without a Request to look at, the "request path" has no meaning.

A Page object has no idea of the paths that will cause it to be called. In fact, it's just a .net class that implements IHttpHandler to generate output.

When a request for a .aspx page comes in, IIS hands it off to the asp.net pipleline. The asp.net engine determines the proper handler based on the path. In this case, it's System.Web.UI.PageHandlerFactory. This class uses the request path to figure out the correct Page object to instantiate. It does this by looking at the request path, and using that to compile the .aspx into .net code and then instantiate the class that compilation results in.

Also unfortunately for your case, the PageHandlerFactory (actually the supporting class BuildManager) allow you to find the information you want in only one direction - virtual path to compiled class. This makes sense because multiple paths could (in theory) map to a single Page.

Check out the any articles on the asp.net request pipeline for more information, or look into the PageHandlerFactory or BuildManager classes in System.Web.

Upvotes: 2

David Andres
David Andres

Reputation: 31781

Try Request.VirtualPath

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351516

System.Web.Page has a Request property that is visible to derived classes.

Upvotes: 0

Related Questions