获取下一页的URL

时间:2013-02-05 15:41:31

标签: c# asp.net url http-redirect

在C#中离开当前页面时是否可以获取下一页的URL?

在我当前页面上,我想在下一页加载之前捕获下一页的网址。我不知道从哪里开始...或者在我的页面上我将放置这个逻辑(例如page_load,buttonClick等)。

3 个答案:

答案 0 :(得分:2)

我认为不可能“捕获下一页的网址”,但您可以尽快获得当前请求。因此请使用Global.asax

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 ...
    }
}

答案 1 :(得分:0)

假设您正在通过查询字符串进行分页,而当前页面是:

url.com/default.aspx?page=1

然后获取下一页网址只会增加页面查询字符串,所以:

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);
}

理想情况下,您将使用UriBuilder重新构建域,而不是像String.Format方法那样对其进行硬编码。我这样做只是为了简洁。

答案 2 :(得分:0)

CodeProject有一个saving change on close or exiting page项目,可能对您有所帮助。

相关问题