Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

setting up a parameter for redirect

I have the following code in my page Load:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string redirector = HttpContext.Current.Request.Url.AbsoluteUri.Remove(0, 7);
            if (redirector.StartsWith("az"))
            {
                Response.Redirect("http://state-solutions.compasslearning.com/statedetails.aspx?id=AZ");
            }
            else if (redirectory.StartsWith("..."))
            {
                etc...
            }
            else if (redirector.StartsWith("tn"))
            {
                Response.Redirect("http://state-solutions.compasslearning.com/statedetails.aspx?id=TN");
            }
            else
            {
                state.DataSource = StateDAO.GetStateList();
                state.DataValueField = "PK";
                state.DataTextField = "name";
                state.DataBind();
                state.Items.Insert(0, new ListItem("----- Please Select -----", ""));
            }
        }
    }

how can I set a parameter for the StartWith("state_abbreviation"), so I don't have to type in every single state_abbreviation for the redirects, which would be about 60 for all the US states and territories?

Thanks for any help.
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

Response.Redirect("http://state-solutions.compasslearning.com/statedetails.aspx?id=" + HttpContext.Current.Request.Url.AbsoluteUri.Remove(0, 7).Substring(0,2));
Avatar of -Dman100-

ASKER

Thanks for replying to my post.

I'm trying to determine how to loop thru the (redirector.StartsWith("state_abbr")?

Because I'd still have 60 else if statements for each state abbreviation
Avatar of kprestage
kprestage

I'm not sure I understand why REA_ANDREW's solution wouldn't work for you.  Have you tried it?  This is basically the same thing, but using your redirector string

Response.Redirect("http://state-solutions.compasslearning.com/statedetails.aspx?id=" + redirector.Substring(0,2));

You would not need any if statements.  This just takes the first 2 characters of your redirector string and adds it to the id=xx parameter of your page that you are redirecting to.   So if redirector = tnxyz, it will redirect to:

http://state-solutions.compasslearning.com/statedetails.aspx?id=tn

if redirector = azxyz, it will redirect to

http://state-solutions.compasslearning.com/statedetails.aspx?id=az

thanks...my mistake.  I had a typo...

This is how I have it coded:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string pointer = HttpContext.Current.Request.Url.AbsoluteUri.Remove(0, 7);
            if (!pointer.StartsWith("state-solutions"))
            {
                Response.Redirect("http://state-solutions.compasslearning.com/statedetails.aspx?id=" + pointer.Substring(0, 2));
            }
            else
            {
                state.DataSource = StateDAO.GetStateList();
                state.DataValueField = "PK";
                state.DataTextField = "name";
                state.DataBind();
                state.Items.Insert(0, new ListItem("----- Please Select -----", ""));
            }
        }
    }

The only thing I'd like to fix, is the hardcoded value for the domain.  So, I don't have to change it when testing moving the files back and forth from my development environment, to the testing environment, to production.

Also, if the domain name ever changed, I'd have to manually make the change.  I was trying to use ResolveURL, but I couldn't get that to work?
Try using Request.ServerVariables("http_host")

so your redirect would be (Request.ServerVariables["http_host"] + "//statedetails.aspx?id=" + pointer.Substring(0, 2));

thanks kprestage...

I tried Request.ServerVariables(http_host)

if (!Page.IsPostBack)
        {
            string pointer = HttpContext.Current.Request.Url.AbsoluteUri.Remove(0, 7);
            string httphost = Request.ServerVariables["http_host"];

            if (!pointer.StartsWith("state-solutions"))
            {
                Response.Redirect("http:///" + httphost + "//statedetails.aspx?id=" + pointer.Substring(0, 2));
            }
            else
            {
                state.DataSource = StateDAO.GetStateList();
                state.DataValueField = "PK";
                state.DataTextField = "name";
                state.DataBind();
                state.Items.Insert(0, new ListItem("----- Please Select -----", ""));
            }
        }

For some reason, the page came up unavailable?  I displayed the value for http_host and everything looks correct.  Strange.  It looks right.  Did I miss anything?
try

Response.Redirect("http:////" + httphost + "//statedetails.aspx?id=" + pointer.Substring(0, 2));
Hi kprestage,

Well, I tried working on it last night and attempted several different approaches.  Still cannot get arond thre hard-coded value.

Here is the code I have in my page Load for my index.aspx page:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string pointer = HttpContext.Current.Request.Url.AbsoluteUri.Remove(0, 7);
            if (!pointer.StartsWith("state-solutions"))
            {
                Response.Redirect("http://state-solutions.compasslearning.com/statedetails.aspx?id=" + pointer.Substring(0, 2));
            }
            else
            {
                state.DataSource = StateDAO.GetStateList();
                state.DataValueField = "PK";
                state.DataTextField = "name";
                state.DataBind();
                state.Items.Insert(0, new ListItem("----- Please Select -----", ""));
            }
        }
    }

As the code is currently, when I run my application in my local development environmnent, the page always goes to my searachdetails.aspx page with the querystring value ?id=lo for localhost.  Whereas, I'd like it to open to my index.aspx page without have to constantly change the value for the pointer variable.  It' working as exepected in the production environment because the hard-coded value is correct.  It's just not correct in the development and testing environment.

So, I'd like to fix the hard-coded value for the domain so I don't have to change it when moving files from my development environment, to the testing environment, to production.  Also, if the domain name ever changed, I'd have to manually make the change.  I was trying to use ResolveURL and then Request.ServerVariables("http_host"), but I couldn't get either to work?

Here is the method I used trying to follow your example:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string pointer = HttpContext.Current.Request.Url.AbsoluteUri.Remove(0, 7);
            string httphost = Request.ServerVariables("http_host");

            if (!pointer.StartsWith("state-solutions"))
            {
                Response.Redirect("http:///" + httphost + "//statedetails.aspx?id=" + pointer.Substring(0, 2));
            }
            else
            {
                state.DataSource = StateDAO.GetStateList();
                state.DataValueField = "PK";
                state.DataTextField = "name";
                state.DataBind();
                state.Items.Insert(0, new ListItem("----- Please Select -----", ""));
            }
        }
    }


Any other options?

Thanks for your help.
I had a typo...there was supposed to be another slash...i.e. ("http:////" + htpphost + ....
Can you give me an example of the desired urls under each circumstance?  Also, using my example, what URL is the page getting redirected to?
Also, what is the value of HttpContext.Current.Request.Url.AbsoluteUri when the code executes?
In my development environment using the current code I have the page goes to:

http://state-solutions.compasslearning.com/statedetails.aspx?id=lo

Whereas, it should go to: http://localhost:1266/Lobbyist/index.aspx 

In the production environment, it is correct.  It goes to: http://state-solutions.compasslearning.com/index.aspx or http://state-solutions.compasslearning.com/statedetails.aspx?id=(with the correct id value) depending on which URL the page is coming from.  For example, if the user types in:
http://tn.compasslearning.com
then the page that is pulled up is http://state-solutions.compasslearning.com/statedetails.aspx?id=TN

Exactly as it is supposed to.  If the user types in http://state-solutions.compasslearning.com/ then it just goes normally to the index page as it should.

So, the issue is really just implementing a value that achieves this both on my local development environment and production so I effectively test and see the results if this is possible?

The value for HttpContext.Current.Request.Url.AbsoluteUri on my local machine is: localhost:1266/Lobbyist/index.aspx

The value for HttpContext.Current.Request.Url.AbsoluteUri in the production environment is: state-solutions.compasslearning.com/index.aspx

Using your example, on my local machine the page comes up unavaiable.  This is the url in the location bar: http://localhost:1266/Lobbyist/index.aspx

Using your example in production and trying the url: http://tn.compasslearning.com/ the page comes up unavailable.

Does this information help?  I tried to include all the information.

Thank you.
So when you run the project locally, it automatically loads to http://localhost:1266/Lobbyist/index.aspx correct?  Then you type in state-solutions.localhost:1266/Lobbyist/index.aspx in the address bar and that is when the problem begins?

ASKER CERTIFIED SOLUTION
Avatar of kprestage
kprestage

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I thought the problem was related to the built-in web server in VS.

I ran the code and got two errors:

'string' does not contain a definition for 'contains'
'string' does not contain a definition for 'contains'

Which occurred on the following lines:

if (httphost.contains("localhost")){
and
httphost = httphost.replace("//index.aspx","");

Am I missing a using directive?  I added using System.IO
my bad.  I am just going off the top of my head, I am not in visual studio.  try httphost.Contains (With an Uppercase C)

Sorry...same with replace = Replace