C# 如何发布然后从 ASP.Net 重定向到外部 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Do I Post and then redirect to an external URL from ASP.Net?
提问by saalon
ASP.NET server-side controls postback to their own page. This makes cases where you want to redirect a user to an external page, but need to post to that page for some reason (for authentication, for instance) a pain.
ASP.NET 服务器端控件回发到它们自己的页面。这使得您想要将用户重定向到外部页面,但由于某种原因(例如,用于身份验证)需要发布到该页面的情况变得很痛苦。
An HttpWebRequest
works great if you don't want to redirect, and JavaScript is fine in some cases, but can get tricky if you really do need the server-side code to get the data together for the post.
HttpWebRequest
如果您不想重定向,An效果很好,并且 JavaScript 在某些情况下很好,但如果您确实需要服务器端代码来为帖子收集数据,则可能会变得棘手。
So how do you both post to an external URL and redirect the user to the result from your ASP.NET codebehind code?
那么您如何发布到外部 URL 并将用户重定向到您的 ASP.NET 代码隐藏代码的结果?
采纳答案by saalon
Here's how I solved this problem today. I started from this articleon C# Corner, but found the example - while technically sound - a little incomplete. Everything he said was right, but I needed to hit a few external sites to piece this together to work exactly as I wanted.
这是我今天解决这个问题的方法。我从这篇关于 C# Corner 的文章开始,但发现这个例子 - 虽然从技术上讲 - 有点不完整。他说的一切都是对的,但我需要访问一些外部网站才能将它们拼凑起来,才能完全按照我的意愿工作。
It didn't help that the user was not technically submitting a form at all; they were clicking a link to go to our support center, but to log them in an http post had to be made to the support center's site.
从技术上讲,用户根本没有提交表单也无济于事;他们点击一个链接进入我们的支持中心,但必须在支持中心的网站上登录 http 帖子。
This solution involves using HttpContext.Current.Response.Write()
to write the data for the form, then using a bit of Javascript on the <body onload="">
method to submit the form to the proper URL.
此解决方案涉及使用HttpContext.Current.Response.Write()
为表单编写数据,然后在<body onload="">
方法上使用一些 Javascript将表单提交到正确的 URL。
When the user clicks on the Support Center link, the following method is called to write the response and redirect the user:
当用户单击支持中心链接时,将调用以下方法来编写响应并重定向用户:
public static void PassthroughAuthentication()
{
System.Web.HttpContext.Current.Response.Write("<body
onload=document.forms[0].submit();window.location=\"Home.aspx\";>");
System.Web.HttpContext.Current.Response.Write("<form name=\"Form\"
target=_blank method=post
action=\"https://external-url.com/security.asp\">");
System.Web.HttpContext.Current.Response.Write(string.Format("<input
type=hidden name=\"cFName\" value=\"{0}\">", "Username"));
System.Web.HttpContext.Current.Response.Write("</form>");
System.Web.HttpContext.Current.Response.Write("</body>");
}
The key to this method is in that onload bit of Javascript, which , when the body of the page loads, submits the form and then redirects the user back to my own Home page. The reason for that bit of hoodoo is that I'm launching the external site in a new window, but don't want the user to resubmit the hidden form if they refresh the page. Plus that hidden form pushed the page down a few pixels which got on my nerves.
此方法的关键在于 Javascript 的 onload 位,它在页面正文加载时提交表单,然后将用户重定向回我自己的主页。出现这种不祥之兆的原因是我在新窗口中启动外部站点,但不希望用户在刷新页面时重新提交隐藏的表单。加上那个隐藏的表单将页面向下推了几个像素,这让我很紧张。
I'd be very interested in any cleaner ideas anyone has on this one.
我对任何人对此有任何更清晰的想法都非常感兴趣。
Eric Sipple
埃里克·西普尔
回答by Mike Powell
If you're using ASP.NET 2.0, you can do this with cross-page posting.
如果您使用的是 ASP.NET 2.0,则可以通过跨页面发布来实现。
Edit: I missed the fact that you're asking about an externalpage. For that I think you'd need to have your ASP.NET page gen up an HTML form whose action is set to the remote URL and method is set to POST. (Using cross-page posting, this could even be a different page with no UI, only hidden form elements.) Then add a bit of javascript to submit the form as soon as the postback result was received on the client.
编辑:我错过了您询问外部页面的事实。为此,我认为您需要让您的 ASP.NET 页面生成一个 HTML 表单,其操作设置为远程 URL,方法设置为 POST。(使用跨页面发布,这甚至可能是一个没有 UI 的不同页面,只有隐藏的表单元素。)然后添加一些 javascript 以在客户端收到回发结果后立即提交表单。
回答by palmsey
I have done this by rendering a form that auto-posts (using JavaScript) to the desired remote URL - gather whatever information you need for the post in the web form's postback and then build the HTML for the remote-posting form and render it back to the client.
我通过呈现一个自动发布(使用 JavaScript)到所需远程 URL 的表单来完成此操作 - 在 Web 表单的回发中收集发布所需的任何信息,然后为远程发布表单构建 HTML 并将其呈现回来给客户。
I built a utility class for this that contains the remote URL and a collection of name/value pairs for the form.
我为此构建了一个实用程序类,其中包含远程 URL 和表单的名称/值对集合。
Cross-page posting will work if you own both of the pages involved, but not if you need to post to another site (PayPal, for example).
如果您拥有所涉及的两个页面,跨页面发布将起作用,但如果您需要发布到另一个站点(例如 PayPal),则不起作用。
回答by sestocker
I would do the form post in your code behind using HttpWebRequest class. Here is a good helper class to get your started:
我会使用 HttpWebRequest 类在你的代码后面做表单发布。这是一个很好的帮助类,可以帮助您入门:
http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
From there, you can just do a Response.Redirect, or perhaps you need to vary your action based on the outcome of the post (if there was an error, display it to the user or whatever). I think you already had the answer in your question to be honest - sounds like you think it is a post OR redirect when in reality you can do them both from your code behind.
从那里,你可以只做一个 Response.Redirect,或者你可能需要根据帖子的结果改变你的操作(如果有错误,将它显示给用户或其他什么)。老实说,我认为您已经在问题中得到了答案 - 听起来您认为这是一个帖子或重定向,而实际上您可以从背后的代码中完成它们。
回答by Kevin
I started with this example from CodeProject
我从CodeProject 的这个例子开始
Then instead of adding to the page, I borrowed from saalon (above) and did a Response.Write().
然后我没有添加到页面,而是从 saalon(上图)借来并做了一个 Response.Write()。
回答by Jenn
I needed to open in the same window, dealing with possible frame issues from the original page, then redirecting to an external site in code behind:
我需要在同一个窗口中打开,处理原始页面中可能的框架问题,然后重定向到代码隐藏的外部站点:
Private Sub ExternalRedirector(ByVal externalUrl As String)
Dim clientRedirectName As String = "ClientExternalRedirect"
Dim externalRedirectJS As New StringBuilder()
If Not String.IsNullOrEmpty(externalUrl) Then
If Not Page.ClientScript.IsStartupScriptRegistered(clientRedirectName) Then
externalRedirectJS.Append("function CheckWindow() {")
externalRedirectJS.Append(" if (window.top != window) {")
externalRedirectJS.Append(" window.top.location = '")
externalRedirectJS.Append(externalUrl)
externalRedirectJS.Append("';")
externalRedirectJS.Append(" return false;")
externalRedirectJS.Append(" }")
externalRedirectJS.Append(" else {")
externalRedirectJS.Append(" window.location = '")
externalRedirectJS.Append(externalUrl)
externalRedirectJS.Append("';")
externalRedirectJS.Append(" }")
externalRedirectJS.Append("}")
externalRedirectJS.Append("CheckWindow();")
Page.ClientScript.RegisterStartupScript(Page.GetType(), clientRedirectName, externalRedirectJS.ToString(), True)
End If
End If
End Sub