1

Currently I have a page that when you fill out a text box and click a button, it redirects you to another page.

The page needs to be loaded, since it updates and shows xml. (I cannot currently change how this is)

However what I what to do is after page was redirected once, redirect it again or just load another page in general.

The thing to note about the xml link, is that part of it is created with the text box, so it will be dynamic.

I currently have something along the lines of this

//please note that username is a textbox, I've just left it out
<script runat = "server">
void Button_Click(Object sender, EventArgs e)
{
    var url = "http://website.com/scripts/" + username.text "/value/0"
    try
    {
        Response.Redirect(url, true);
    }
    catch(Exception ex)
    {//From what I learnt, adding true to redirect throws an exception,
     //which is how I tried executing another redirect, but it doesn't seem to
     //to load the first direct, and skips straight to this, I also put this
     //in finally, because it seemed more appropriate to no avail
        Response.Redirect(someurl, true);
    }
 }

So I'm wondering if this is actually possible, I also wonder if I'm just looking up the wrong keywords to find a solution.

I've spent a bit of time on this, and have yet to come to some sort of solution, but I'm new to web development so I may just be missing some incredibly simple.

Also I only really understand how C# works in asp, but am willing to learn how to add in javascript or VB if necessary.

Thanks in advance for the help

Edit: Solution!

So I managed to use javascript to append the textbox value to the xml link, request it and without showing the user (showing the user, is not necessary in this case).

After which a popup confirms that it is successful then reloads the page.

it is very self explanatory but what I did was

url = "website";
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, true);
window.alert("success");
return true;//this reloads the page, that or just window.location.reload();

For an added check, I will see if I can verify that the username is a valid username, and popup with failure text if not.

Mark Manson
  • 125
  • 1
  • 12
  • Your problem is bit vague. if you are asking can you do a redirect within a redirect, then yes. – Baahubali Jan 24 '17 at 03:13
  • Oh sorry about that, how do I do that exactly? – Mark Manson Jan 24 '17 at 03:16
  • what issues are you having doing this? once the action is performed on your first redirect page, just call Response.Redirect again or Server.Transfer depending on your requirements. – Baahubali Jan 24 '17 at 03:18
  • Well checking the xml link (I use another external way of checking this) if I do the following it doesn't properly update the xml, so it seems like I have to load the first link which is a bit of a pain, but I can't really change how the xml works – Mark Manson Jan 24 '17 at 03:22

3 Answers3

1

You seem to have a misunderstanding about what Response.Redirect(...) actually does. The method name is, in my opinion, a bit misleading. It suggests that somehow the Response to the currently executing request will be sent somewhere else than the requesting browser. This is not the case. The name could as well have been Response.SendRedirectResponseToBrowser, because that's what Response.Redirect does.

So when you do Response.Redirect(url) you are telling the server that is executing your page that is should send a response to the browser, telling the browser to do a GET request of the supplied url. The browser will then do that, at which point that page needs to include a separate Redirect in order to further tell the browser where to go next.

In this case then, the page at "http://website.com/scripts/" + username.text "/value/0" needs to be patched up so that after processing the request, it will also send a redirect response with the url you want to display next.

If you have no control over that page, then you must solve this some other way. Some options:

  • Use ajax to request the "http://website.com/scripts/" + username.text "/value/0" url. Then after completion set the page location to the url you want to show next.
  • Open the http://website.com/.... url in a _blank target, then set to location to the next page.
  • Use System.Net.Http.HttpClient in your code behind method to request the http://website.com/.... url, then do a redirect. This means that the server requests the url as part of processing the button click.

Notes:

  • If the http://website.com/.... url updates some state (like store some changes in a database or similar), then you should request it using a POST request, not a GET. GET requests can get a cached response which means that the server might never actually see the request, and therefore not do any processing.
  • Piecing together the url like this "http://website.com/scripts/" + username.text "/value/0" looks risky. You should at the very minimum url encode the username.text - HttpUtility.UrlEncode(username.text). Better yet would be the first validate that the entered username is actually a valid user name.
user1429080
  • 9,086
  • 4
  • 31
  • 54
  • Thanks for clearing up the misunderstanding, I never really had much of a web development concept back at university, and currently don't have the luxury of properly learning web development from a better foundation. I honestly don't like how this server was created, and it was done before my employment. Plus with my little knowledge of web development and server development as a whole I wouldn't want to try rebuilding it. The xml link is very arbitrary, I actually came across it searching through the server, a method that is never used, it's real use is just a true/false value, nothing more. – Mark Manson Jan 24 '17 at 19:05
  • I will go ahead and give this a go as well, again I appreciate the explanation. – Mark Manson Jan 24 '17 at 19:06
  • Thank you so much!, I managed to use javascript to request the page, send null and then finally refresh the page after a small popup. It works the way I needed. – Mark Manson Jan 25 '17 at 01:38
0

You can add a Refresh header (not a meta-refresh element) to the response that contains the XML. In the header, you can specify another URL and the number of seconds to wait before redirecting.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • I tried doing Response.AddHeader("REFRESH", "2, URL="website""); in the finally but that didn't seem to work, I'm guessing this isn't the correct way to do it though. – Mark Manson Jan 24 '17 at 03:29
0

I guess it should be using JavaScript (front-end) instead of back-end error handling, because it goes to another page. Use promise to handle exception

  • Thanks I'll give it a go tomorrow, I literally finished work so if it definitely works out I'll mark this as the answer – Mark Manson Jan 24 '17 at 03:40
  • Thanks for your answer, this was definitely along the lines of how I got things done, I wish I could mark it as part of the answer, – Mark Manson Jan 25 '17 at 02:03