问题描述
我有发布数据,并重定向到外部URL的麻烦。外部URL是只接受提交的表单使用发表方式在线支付网关。
I am having trouble with posting data and redirecting to an external URL. The external URL is an online payment gateway that only accepts submitted form using POST method.
像下面简单的HTML表单将工作没有任何问题。
A simple html form like below will work without any issues
<html>
<body>
<form action="externalpage.url" method="post">
<input type="hidden" name="name1" value="1234">
<input type="hidden" name="name2" value="abcd">
<input type="submit" value="Submit Form">
</form>
</body>
</html>
但在我的情况下,当用户点击我的aspx页面的按钮,我需要首先做一些服务器端处理,如创建一个的NameValueCollection
对象,重定向她支付网关之前。
But in my scenario, when the user clicks the button on my aspx page, I need to do some server side processing first, e.g. create a NameValueCollection
object, before redirecting her to the payment gateway.
我试图用这个例子此链接:,但页面不重定向到外部URL部分原因。的code以下行似乎并没有做任何事情。
I have tried to use the example from this link: http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET, but the page doesn't redirect to the external URL for some reason. The following line of code doesn't seem to do anything.
page.Controls.Add(new LiteralControl(strForm));
那么如何解决这个问题有什么建议?谢谢。
So any suggestions on how to solve this issue? Thanks.
推荐答案
我想我想通了,如何解决这个问题。这是我做了什么。
I think I figured out how to resolve this issue. Here is what I have done.
创建一个中间页,例如DoPost.aspx。
Create an intermediate page, e.g. DoPost.aspx.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Post Form</title>
<script>
function PostData() {
document.getElementById("form1").submit();
}
</script>
</head>
<body onload="PostData()">
<form id="form1" runat="server">
<div>
<asp:Literal ID="ltrPostData" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
然后在后面的code,
Then in the code behind,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var url = "external.url";
form1.Action = url;
ltrPostData.Text = "Create <input type='hidden' name='data name' value='data value' /> html controls from Request.QueryString() and assign the string value to the Literal control's Text property.";
}
}
在Submit.aspx,在按钮点击事件发生时,
On Submit.aspx, where the button click event happens,
//assume that the NameValueCollection object has already been created and populated
var redirectUrl = string.Format("{0}?{1}", "DoPost.aspx", BuildQueryString(data));
Response.Redirect(redirectUrl, true);
public static string BuildQueryString(NameValueCollection data)
{
// create query string with all values
return string.Join("&", data.AllKeys.Select(key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(data[key]))));
}
那么有效,我们仍然可以使用的Response.Redirect()将数据传递给外部的链接,虽然我们需要通过第二个页面。总之,Submit.aspx [GET] => DoPost.aspx [发布] =>网关
So effectively, we could still use Response.Redirect() to pass the data to the external link although we will need to go through a second page. In short, Submit.aspx [GET] => DoPost.aspx [Post] => The Gateway
这篇关于如何发布数据,并重定向到一个外部网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!