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

ASP.NET C# code for Page direction dynamically based on database value

I am designing a web application on ASP.NET  with C# code.
I wish to redirect to respective homepages based on userids. The userlogin table has a field “pagedirection” to indicate the page for redirection. For example,
for “visitor” userid the pagedirection value is
"../visitor/visitorhomepage.aspx"
In the usual non-dynamic mode one uses the following code
Response.Redirect("../visitor/visitorhomepage.aspx");

The following code to redirect dynamically is not working.
Response.Redirect(dbreader["pagedirection"].ToString());

 Can anyone modify the code to get the results that I desire.
0
Srinivas Mantha
Asked:
Srinivas Mantha
  • 3
  • 3
1 Solution
 
guru_samiCommented:
I would first see if the correct value is returned for pagedirection.
Add this and set a breakpoint and see:

string url  = dbreader["pagedirection"].ToString();
Response.Redirect(url); //set a breakpoint and see what is the url that you are getting?

most likely you will need to have your url in ~/ format.
0
 
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
The url is returning the expected value at breakpoint i.e.
"../visitor/visitorhomepage.aspx"
However at the end the following error message is displayed:

Server Error in '/websitesmnew' Application.
HTTP Error 400 - Bad Request.
0
 
guru_samiCommented:
And what do you see in the address bar for url when the error is thrown?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
I am seeing the following in the address bar
http://localhost:49212/websitesmnew/smnew/%22../visitor/visitorhomepage.aspx%22

when I commented the dyanamic mode and activated nondynamic mode, it is directed to the correct desired page in which address bar is displaying

http://localhost:49212/websitesmnew/visitor/visitorhomepage.aspx
0
 
guru_samiCommented:
ok so you need to remove the " (double quotes) and .. from your url and add a ~ before doing a Redirect.

I would suggest you to store url without " and .. in your db e.g.
 /visitor/visitorhomepage.aspx

string url  = dbreader["pagedirection"].ToString();
Response.Redirect("~/" + url.TrimStart('/'));
0
 
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
Thank you for the accurate solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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