在MVC 4中使用POST参数进行重定向,以实现Payment Gateway集成 [英] Redirect with POST parameters in MVC 4 for Payment Gateway Integration

查看:217
本文介绍了在MVC 4中使用POST参数进行重定向,以实现Payment Gateway集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用剃须刀中的mvc4进行支付网关集成.在这种情况下,我需要调用一个带有预填帖子表格的页面.

I am trying to do a payment gateway integration using mvc4 in razor. In that i need to call a page with prefilled post form.

使用以下方法,我将形成后方法表格:

Using the below method, I am forming the post method form:

private static string PreparePOSTForm(string url, System.Collections.Hashtable data)      // post form
    {
        //Set a name for the form
        string formID = "PostForm";
        //Build the form using the specified data to be posted.
        StringBuilder strForm = new StringBuilder();
        strForm.Append("<form id=\"" + formID + "\" name=\"" +
                       formID + "\" action=\"" + url +
                       "\" method=\"POST\">");

        foreach (System.Collections.DictionaryEntry key in data)
        {

            strForm.Append("<input type=\"hidden\" name=\"" + key.Key +
                           "\" value=\"" + key.Value + "\">");
        }

        strForm.Append("</form>");
        //Build the JavaScript which will do the Posting operation.
        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script language='javascript'>");
        strScript.Append("var v" + formID + " = document." +
                         formID + ";");
        strScript.Append("v" + formID + ".submit();");
        strScript.Append("</script>");
        //Return the form and the script concatenated.
        //(The order is important, Form then JavaScript)
        return strForm.ToString() + strScript.ToString();
    }

在控制器"页面中,我使用必需的参数调用PreparePostForm,并且收到POST请求格式.

And in my Controller page I am calling the PreparePostForm with required parameter and I am receiving the POST request format.

[HttpPost]
 public ActionResult OrderSummary()
        {
            string request=PreparePOSTForm("payment URL","hashdata required for payment")
            return Redirect(request);
        }

但是在重定向时,我遇到了以下错误.

But while redirecting I am getting below error.

错误请求-无效的URL

Bad Request - Invalid URL

HTTP错误400.请求URL无效.

HTTP Error 400. The request URL is invalid.

我在这里缺少一些与POST请求一起使用的东西.有人可以帮我吗?

I am missing something here to work with POST request. Can someone help me.

谢谢.

推荐答案

您不能通过Redirect方法发布表单.您可以将生成的表单字符串发送到View,然后由Javascript发布表单.

You cannot post a form by Redirect method. You could send generated form string to View and after that post the form by Javascript.

public ActionResult OrderSummary()
{
    string request=PreparePOSTForm("payment URL","hashdata required for payment")
    return View(model:request);
}

以及OrderSummary的视图:

@model string

@Html.Raw(Model)

<script>
    $(function(){
       $('form').submit();
    })
</script>

这篇关于在MVC 4中使用POST参数进行重定向,以实现Payment Gateway集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆