简体   繁体   English

在加载另一个页面后重定向到一个页面之后

[英]After Redirecting to a page after loading another page

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.该页面需要加载,因为它会更新并显示 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.关于 xml 链接需要注意的是,它的一部分是用文本框创建的,因此它将是动态的。

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.我在这方面花了一些时间,还没有找到某种解决方案,但我是 Web 开发的新手,所以我可能只是错过了一些非常简单的东西。

Also I only really understand how C# works in asp, but am willing to learn how to add in javascript or VB if necessary.此外,我只真正了解 C# 在 asp 中的工作原理,但我愿意在必要时学习如何添加 javascript 或 VB。

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).所以我设法使用 javascript 将文本框值附加到 xml 链接,请求它而不显示用户(在这种情况下不需要显示用户)。

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.对于附加检查,我将查看是否可以验证用户名是否为有效用户名,如果不是,则弹出失败文本。

You seem to have a misunderstanding about what Response.Redirect(...) actually does.您似乎对Response.Redirect(...)实际作用有误解。 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.它表明以某种方式对当前正在执行的请求的Response将被发送到请求浏览器之外的其他地方。 This is not the case.情况并非如此。 The name could as well have been Response.SendRedirectResponseToBrowser , because that's what Response.Redirect does.该名称也可以是Response.SendRedirectResponseToBrowser ,因为这就是Response.Redirect所做的。

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 .因此,当您执行Response.Redirect(url)您是在告诉正在执行您的页面的服务器应该向浏览器发送响应,告诉浏览器对提供的url执行GET请求。 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.然后浏览器会这样做,此时页面需要包含一个单独的Redirect ,以便进一步告诉浏览器下一步要去哪里。

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.在这种情况下, "http://website.com/scripts/" + username.text "/value/0"需要进行修补,以便在处理请求后,它还将发送重定向响应接下来要显示的网址。

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.使用ajax请求"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._blank目标中打开http://website.com/.... url,然后将位置设置为下一页。
  • Use System.Net.Http.HttpClient in your code behind method to request the http://website.com/.... url, then do a redirect.在您的代码隐藏方法中使用System.Net.Http.HttpClient来请求http://website.com/.... url,然后进行重定向。 This means that the server requests the url as part of processing the button click.这意味着服务器请求 url 作为处理按钮点击的一部分。

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 .如果http://website.com/.... url 更新了一些状态(比如在数据库中存储一些更改或类似的),那么你应该使用POST请求而不是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. GET请求可以获得缓存的响应,这意味着服务器可能永远不会真正看到请求,因此不会进行任何处理。
  • Piecing together the url like this "http://website.com/scripts/" + username.text "/value/0" looks risky.像这样拼凑 url "http://website.com/scripts/" + username.text "/value/0"看起来有风险。 You should at the very minimum url encode the username.text - HttpUtility.UrlEncode(username.text) .您应该至少对username.text进行 url 编码 - HttpUtility.UrlEncode(username.text) Better yet would be the first validate that the entered username is actually a valid user name.更好的是首先验证输入的用户名实际上是一个有效的用户名。

You can add a Refresh header ( not a meta-refresh element) to the response that contains the XML.您可以向包含 XML 的响应添加Refresh 标头不是元刷新元素)。 In the header, you can specify another URL and the number of seconds to wait before redirecting.在标头中,您可以指定另一个 URL 和重定向前等待的秒数。

I guess it should be using JavaScript (front-end) instead of back-end error handling, because it goes to another page.我想它应该使用 JavaScript(前端)而不是后端错误处理,因为它转到另一个页面。 Use promise to handle exception使用promise处理异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM