[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to redirect in aspx page in c#

Posted on 2012-08-23
9
Medium Priority
?
986 Views
Last Modified: 2012-09-10
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");

        }

%>
0
Comment
Question by:mmalik15
9 Comments
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38325710
Because you are working a server side push, you can try:

Server.Transfer("Webform2.aspx");

Instead of Response.Redirect.

See if that helps.
0
 

Author Comment

by:mmalik15
ID: 38325722
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
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38325821
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 23

Expert Comment

by:Christopher Kile
ID: 38326391
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
 

Author Comment

by:mmalik15
ID: 38327321
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
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38327352
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
 

Author Comment

by:mmalik15
ID: 38327372
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
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38327378
Check out the file I gave you.
I tested it, and it went to the site as requested.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 38330563
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question