how to redirect in aspx page in c#

I need to redirect a page to another page directly on the server. i.e. any request to this page
http://www.elib.scot.nhs.uk/SharedSpace/rehab/Pages/Index.aspx?ContainerID=200298
should go to http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx

I can't make change in the .cs file. I am trying to add something like this in aspx file but no luck

<%

    if (!String.IsNullOrEmpty(Request.QueryString["ContainerID"]))
        {
            var x = Request.QueryString["ContainerID"].ToString();
            if (x == "200298")
                Response.Redirect("http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx");

        }

%>
mmalik15Asked:
Who is Participating?
 
Todd GerbertIT ConsultantCommented:
Although rare, it is possible that a client will have JavaScript disabled, plus then a user could examine the JavaScript code and learn how to avoid the redirect (if that's even a concern), and I just find the code-behind solution much easier to implement & change. ;)

This bit works for me fine:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
	<%
		if (!String.IsNullOrEmpty(Request["ContainerID"]))
		{
			string containerId = Request["ContainerID"] as string;

			if (containerId.Trim() == "200298")
				Response.Redirect("http://www.google.com/");
		}
	 %>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Blah blah blah...
    </div>
    </form>
</body>
</html>

Open in new window


Note, however, that this will only work for requests made with GET - if you have a need to catch POST's you'll need to examine Request.Form (I'm pretty sure, anyway, been a while since I've done ASP.Net).
0
 
Evan CutlerVolunteer Chief Information OfficerCommented:
Because you are working a server side push, you can try:

Server.Transfer("Webform2.aspx");

Instead of Response.Redirect.

See if that helps.
0
 
mmalik15Author Commented:
sorry it does not work I have tried the following

<%@ Page language="c#" Codebehind="Index.aspx.cs" AutoEventWireup="false" Inherits="nes.SharedSpace.Pages.Index" %>


<%

    if (!String.IsNullOrEmpty(Request.QueryString["ContainerID"]))
        {
            var x = Request.QueryString["ContainerID"].ToString();
            if (x == "200298")
                Server.Transfer("http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx");

        }

%>

and when I click on http://www.elib.scot.nhs.uk/SharedSpace/rehab/Pages/Index.aspx?ContainerID=200298 I get a blank page
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Evan CutlerVolunteer Chief Information OfficerCommented:
ok...so this is inline ASP?  as in ASP Classic?

And are you looking at this as part of the page load?
or as part of a change in status to a control on the page?

One more thing:
I tried to navigate to:
http://www.elib.scot.nhs.uk/SharedSpace/rehab/Pages/Index.aspx 
without the ?= element, and that page itself does not load.

Is that page missing?
Thanks
0
 
Christopher KileCommented:
If you can't make a change in the .cs file of your code-behind, why make this a server-side script?

I'm going to assume you are using Javascript for your client script.  Replace your <% %> block with:

<script type="text/javascript" language="javascript">
    if ("<%=Request.QueryString["ContainerID"].ToString()%>" == "200298")
        window.navigate("http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx");
</script>

Open in new window


Alternatively, you can use

<script type="text/javascript" language="javascript">
    if ("<%=Request.QueryString["ContainerID"].ToString()%>" == "200298")
        window.location = "http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx";
</script>

Open in new window

0
 
mmalik15Author Commented:
My aspx page now have the following markup

<%@ Page language="c#" Codebehind="Index.aspx.cs" AutoEventWireup="false" Inherits="nes.SharedSpace.Pages.Index" %>

<script type="text/javascript" language="javascript">
    if ("<%=Request.QueryString["ContainerID"].ToString()%>" == "200298")
        window.navigate("http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx");
</script>

now I am getting an error on the page. Please click on the page to see the exact error

http://www.elib.scot.nhs.uk/SharedSpace/rehab/Pages/Index.aspx?ContainerID=200298
0
 
Evan CutlerVolunteer Chief Information OfficerCommented:
Let's change it up a bit:

<script type="text/javascript">
var first = getUrlVars()["ContainerID"];
if (first == 200298)
{
	window.location.href = 'http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx';
}

	


function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}


</script>

Open in new window

test.html
0
 
mmalik15Author Commented:
I have  changed the script but still getting the last error.

<script type="text/javascript">
var first = getUrlVars()["ContainerID"];
if (first == 200298)
{
      window.location.href = 'http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx';
}

      


function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}


</script>
0
 
Evan CutlerVolunteer Chief Information OfficerCommented:
Check out the file I gave you.
I tested it, and it went to the site as requested.
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.

All Courses

From novice to tech pro — start learning today.