After logon, the user jumps to the URL jump before logon.

Source: Internet
Author: User

In websites with user logon, there will be pages that require user logon before operation. If a user can jump to the previous page after logon, it will give the user a good experience, it also makes it easier for them to browse pages to be operated. In the middle, you need to obtain the URL parameter, which is used for redirection.

Main steps:

For better illustration, there are already two pages, order. aspx and login. aspx.

1. Order. aspx is used to implement the order page. The main function is to submit an order when a user selects a product. However, the user must log in before submitting the order ..

It is indicated by code. It is more clear that when loading to the order. ASPX page, it determines whether the user is logged on. This judges using session to judge

Protected void page_load (Object sender, eventargs e) <br/>{< br/> string returnurl = request. URL. absoluteuri; // obtain the current URL <br/> If (session ["user"] = NULL) <br/>{< br/> response. redirect ("/login. aspx? Url = "+ server. urlencode (returnurl); <br/>}< br/> else <br/>{< br/> // perform the order operation, this user has logged on <br/>}</P> <p>}

The above mainly implements the transfer of the URL address of the current page to the login page

2. login. aspx enables the user to log on and obtain the passed URL parameters.

The Code is as follows:

// Logon method <br/> private bool login () <br/>{< br/> // user verification <br/> //...... </P> <p> // determine the jump <br/> If (! String. isnullorempty (request. querystring ["url"]) <br/>{< br/> // jump to the pre-Logon page <br/> string returnurl = request. querystring ["url"]. tostring (); <br/> response. redirect (server. urldecode (returnurl )); <br/>}< br/> else <br/>{< br/> // logon page <br/>}< br/>}

You only need a simple jump to achieve this. If there are many similar operations, it is best to encapsulate them into a unified method call.

 

Note: If the page to be redirected is under the virtual directory rather than the root site directory,If the settings are different, you must make a judgment...

In this case, you need to determine the URL virtual directory.

/// <Summary> <br/> // determine whether the URL path is the root directory <br/> /// </Summary> <br/> // <returns> </returns> <br/> Public static string getrooturl () <br/>{< br/> string apppath = ""; <br/> httpcontext httpcurrent = httpcontext. current; <br/> httprequest req; <br/> If (httpcurrent! = NULL) <br/>{< br/> Req = httpcurrent. request; </P> <p> string urlauthority = req. URL. getleftpart (uripartial. authority); </P> <p> If (req. applicationpath = NULL | req. applicationpath = "/") <br/> // directly installed on the web site <br/> apppath = urlauthority; <br/> else <br/> // install it in the virtual subdirectory <br/> apppath = urlauthority + req. applicationpath; <br/>}< br/> return apppath; <br/>}

Code Note: req. URL. getleftpart (uripartial. authority); Return http: // localhost in the root directory, if it is in the virtual directory (the virtual directory is set to Web), the returned http: // localhost/Web

In this way, the return value of Req. url. getleftpart (uripartial. Authority) will be added to the order. apsx page before the jump is executed.

For example, string returnurl = Req. url. getleftpart (uripartial. Authority) + request. url. absoluteuri; // obtain the current URL

In this way, you can correctly execute the jump...

 

Appendix:

The following is a comparison of various methods for obtaining URL Information from a request.

From: http://www.cnblogs.com/dudu/archive/2004/02/20/1435.html

The URL of the test is a http://www.test.com/testweb/default.aspx and the result is as follows:

Request. applicationpath:/testweb
Request. currentexecutionfilepath:/testweb/default. aspx
Request. filepath:/testweb/default. aspx
Request. Path:/testweb/default. aspx
Request. pathinfo:
Request. physicalapplicationpath: e:/www/testweb/
Request. physicalpath: e:/www/testweb/default. aspx
Request. rawurl:/testweb/default. aspx
Request. url. absolutepath:/testweb/default. aspx
Request. url. absoluteuri: http://www.test.com/testweb/default.aspx
Request. url. HOST: www.test.com
Request. url. localpath:/testweb/default. aspx

In summary, to achieve the basic implementation of login jump, the main purpose is to use the URL parameter to determine before the jump, and then get it at login ..

It is important to know that the request. URL has different attributes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.