user2030579
user2030579

Reputation: 41

Getting URL of next page

Is it possible to get the URL of the next page when leaving the current page in C#?

On my current page I would like to capture the url of the next page before the next page loads. I'm not sure where to start for this...or in which method on my page I would place this logic (such as page_load, buttonClick etc.)

Upvotes: 0

Views: 1974

Answers (3)

comecme
comecme

Reputation: 6386

CodeProject has a project for saving change on close or exiting page which might be helpful to you.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460048

I don't think that it's possible to "capture the url of the next page", but you can get the current request as soon as possible. Use Global.asax therefore:

public class Global : System.Web.HttpApplication
{
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = base.Context;
        HttpResponse response = context.Response;
        HttpRequest request = context.Request;
        var url = request.RawUrl;
        // and many other properties ...
    }
}

Upvotes: 2

Jack Marchetti
Jack Marchetti

Reputation: 15754

Assuming you're doing paging through query strings and your current page is:

url.com/default.aspx?page=1

Then getting the next page URL would just be incrementing the page query string, so:

if (!String.IsNullOrempty(Request.QueryString["page"]) {
  int nextPageNumber = int.Parse(Request.QueryString["page"] + 1; 
  string nextPageUrl = String.Format("url.com/default.aspx?page={0}, nextPageNumber);
}

Ideally you would use UriBuilder to reconstruct the domain and not hard code it as I have in the String.Format method. I did this just for brevity.

Upvotes: 0

Related Questions