• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 938
  • Last Modified:

Redirecting request from one page to another page in asp.net

Hello dear experts
I have two aspx pages (page 1 and 2), I want to send the request to page two from page one, I mean that I want to avoid access to page 2 when a user directly going to page 2 and the user should go to page 2 from page 1 and should be redirected to page 1 when requesting directly to see the page 2.
Please guide me how I could achieve this task.
Thanks


0
alirezakaveh
Asked:
alirezakaveh
1 Solution
 
BlomholmCommented:
This should work
//In page 1 this would transfer to page 2:
Server.Transfer("./submitreport.aspx");
 
//In page 2 in page_load:
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack && PreviousPage != null)
    {
         //do stuff
    }
    else {
        Response.write("not allowed to access directly")
    }

Open in new window

0
 
Richard LeeSoftware EnthusiastCommented:
In your page load event in page 2 do the following:

// Check if page was directly accessed
if(String.IsNullOrEmpty(Request.UrlReferrer.ToString()))
{
    Response.Redirect("Page1.aspx");
}

// Check if the page had a referrer but not the correct one
if(!Request.UrlReferrer.GetLeftPart(UriPartial.Path).ToLower().Contains("page1.aspx"))
{
      Response.Redirect("Page1.aspx");
}
0
 
Richard LeeSoftware EnthusiastCommented:
The above could be re-written with the PreviousPage property as follows:
// Check if page was directly accessed
if(String.IsNullOrEmpty(PreviousPage.Request.Url.ToString()))
{
    Response.Redirect("Page1.aspx");
}
 
// Check if the page had a referrer but not the correct one
if (!PreviousPage.Request.Url.GetLeftPart(UriPartial.Path).ToLower().Contains("page1.aspx"))
{
	Response.Redirect("Page1.aspx");
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sunithnairCommented:
Better to use Request.UrlReferrer as DaTribe mentioned in the 2nd post here. PreviousPage will be null if you do a response.redirect, it will have value only if it is a server.transfer
0
 
nmarunCommented:
When it comes to Response.Redirect, always use the overloaded method:

Response.Redirect("NewPage.aspx", false);

http://support.microsoft.com/kb/312629
0
 
alirezakavehAuthor Commented:
I am getting NullReferenceException error and if i use try catch nothing will happen
0
 
Richard LeeSoftware EnthusiastCommented:
Try the following
// Check if page was directly accessed
if(Request.UrlReferrer == null || String.IsNullOrEmpty(Request.UrlReferrer.ToString()))
{
    Response.Redirect("Page1.aspx");
}
 
// Check if the page had a referrer but not the correct one
if(Request.UrlReferrer == null || !Request.UrlReferrer.GetLeftPart(UriPartial.Path).ToLower().Contains("page1.aspx"))
{
      Response.Redirect("Page1.aspx");
}

Open in new window

0
 
alirezakavehAuthor Commented:
Thanks it worked
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now