简体   繁体   English

使用MVC 4中的POST参数重定向以进行支付网关集成

[英]Redirect with POST parameters in MVC 4 for Payment Gateway Integration

I am trying to do a payment gateway integration using mvc4 in razor. 我正在尝试使用剃刀中的mvc4进行支付网关集成。 In that i need to call a page with prefilled post form. 因为我需要调用带有预填表格的页面。

Using the below method, I am forming the post method form: 使用下面的方法,我正在形成post方法表单:

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();
    }

And in my Controller page I am calling the PreparePostForm with required parameter and I am receiving the POST request format. 在我的Controller页面中,我使用必需参数调用PreparePostForm ,并且我正在接收POST请求格式。

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

But while redirecting I am getting below error. 但是在重定向时我的误差低于误差。

Bad Request - Invalid URL 错误请求 - 无效的网址

HTTP Error 400. The request URL is invalid. HTTP错误400.请求URL无效。

I am missing something here to work with POST request. 我在这里错过了一些与POST请求一起工作的东西。 Can someone help me. 有人能帮我吗。

Thanks in advance. 提前致谢。

You cannot post a form by Redirect method. 您无法通过Redirect方法发布表单。 You could send generated form string to View and after that post the form by Javascript . 您可以将生成的表单字符串发送到View ,然后通过Javascript发布表单。

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

and in View of OrderSummary : 并在OrderSummary视图中:

@model string

@Html.Raw(Model)

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

I suggest you to create an Action with form which receives a Model in parameters. 我建议你创建一个Action with form,它在参数中接收Model Then, just pass model when you redirect to this Action 然后,只需在重定向到此Action时传递模型

[HttpPost]
public ActionResult OrderSummary()
{
    return RedirectToAction("OrderForm", new { HashData = hashData });
}

[HttpGet]
public ViewResult OrderForm(string hashData)
{
    OrderFormModel model = new OrderFormModel();
    model.HashData = hashData;
    return View(model);
}

[HttpPost]
public ActionResult OrderForm(OrderFormModel model)
{
    if(ModelState.IsValid)
    {
        // do processing
    }
}

You can do it with JavaScript. 你可以用JavaScript做到这一点。

Make a page which does have a html form and submit it through javascript and put your information as input:hidden in the form. 创建一个具有html表单的页面并通过javascript提交并将您的信息作为输入:隐藏在表单中。

This will submit the data to the another place you want. 这会将数据提交到您想要的其他地方。 Doing it in html gave you more control and you doesn't need to write a action like shown in other answer for every redirect in your app. 在html中执行此操作可以为您提供更多控制权,并且您无需为应用中的每个重定向编写其他答案中显示的操作。

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

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