Proviance
Proviance

Reputation:

Get URL of ASP.Net Page in code-behind from another page

I am trying to find away to determine a pages url from the page object. It seems you can only get the path of the current context.

Upvotes: 5

Views: 4317

Answers (4)

Jeffrey Blake
Jeffrey Blake

Reputation: 9709

If the page you are seeking is in the same directory as the current page, or a subdirectory of it, then you can use something like the following (adding the sub-directory page into the string, as needed):

string targetUrl =  HttpContext.Current.Request.Url.Scheme
                 + "://"
                 + HttpContext.Current.Request.Url.Authority
                 + this.TemplateSourceDirectory + "/Page.aspx";

Upvotes: 0

iZ.
iZ.

Reputation: 5041

It should be available via

string currentUrl = Page.Request.Url.ToString();

Upvotes: 0

Glennular
Glennular

Reputation: 18225

Hopefully one of these will help you.

Understanding Paths in ASP.NET

Expression - Evaluation

this.TemplateSourceDirectory - /informit/subdir

Request.MapPath("log.txt") - c:\mywebdirs\informit\subdir\log.txt

this.MapPathSecure("log.txt") - c:\mywebdirs\informit\subdir\log.txt

Request.Path - /informit/subdir/pathsample.aspx/extra

Request.FilePath - /informit/subdir/pathsample.aspx

Request.CurrentExecutionFilePath - /informit/subdir/pathsample.aspx

Request.PathInfo - /extra

Request.PhysicalPath - c:\mywebdirs\informit\subdir\pathsample.aspx

Request.PhysicalApplicationPath - c:\mywebdirs\informit\

Request.ApplicationPath - /informit

Request.Url - http://localhost/informit/subdir/client.aspx/extra

Request.RawUrl - /informit/subdir/pathsample.aspx/extra

Response.ApplyAppPathModifier("foo.aspx") - /informit/subdir/foo.aspx

this.ResolveUrl("~/client.aspx") - /informit/pathsample.aspx

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416159

A page can have several urls. For example, I have a server running at home and the site's url is different depending on where I check it from. When I'm at home I just use the internal server name so that the traffic never leaves my home network. When I'm elsewhere I have to use the dyndns.org -based url. I could also configure several different sites to all point to the same place. The point is that this information is not tied to your page's class type, or even a specific instance.

Therefore, the URL of a page can only be determined on a per-request basis, and sure enough you can get it by looking at Request.Url

Upvotes: 2

Related Questions