1

In ASP.Net Core 2.0, how can I POST data while redirecting to a third-party URL using Response.Redirect()?

Note: I have to POST this data without using the query-string.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
  • I have to redirect the url with data to third party payment gateway. So how can I achieve this using Response.Redirect() method in ASP.Net Core 2.0? or else if you have anyother ideas please suggest me. – Alexandar Rajavel Sep 11 '18 at 11:05
  • For example my third party pamentgateway url is: http://xxxx:xxxx/PaymentGatewayWrapper.aspx var requestData="some data"; Dictionary postData = new Dictionary(); postData.Add("requestToJson", requestData); Finaly I have to redirect to above url with this postData – Alexandar Rajavel Sep 11 '18 at 11:12

3 Answers3

1

Response.Redirect triggers a GET request which means that the only option is using a query string. Can you trigger the redirection from the client (if any) in order to make a POST request?

A. Roussos
  • 268
  • 3
  • 11
  • I have to redirect the url with data to third party payment gateway. So how can I achieve this using Response.Redirect() method in ASP.Net Core 2.0? or else if you have anyother ideas please suggest me. – Alexandar Rajavel Sep 11 '18 at 11:03
  • Take a look at this answer https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request/4015346#4015346 – A. Roussos Sep 14 '18 at 07:52
0

You must use object if you want post data without using query string.

[HttpPost]
public IActionResult Search([FromBody] CustomerSearchRequestApiModel request)
{       
    if (request == null)
    {
        return BadRequest();    
    }

    return Ok(request);
}
Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Haktan Enes Biçer
  • 612
  • 1
  • 10
  • 13
  • I have to redirect the url with data to third party payment gateway. So how can I achieve this using Response.Redirect() method in ASP.Net Core 2.0? or else if you have anyother ideas please suggest me. – Alexandar Rajavel Sep 11 '18 at 11:02
0

It is impossible to use Response.Redirect() to send Post request.

For a workaround, you could try HttpClient to send Post request and then return the reponse to the web browser with ContentResult as text/html.

Here is a demo code:

        public async Task<ContentResult> HtmlView()
    {
        using (var formDataContent = new MultipartFormDataContent())
        {
            HttpClient client = new HttpClient();
            Article article = new Article { ArticleName = "AN" };

            formDataContent.Add(new StringContent("AN", Encoding.UTF8, "application/json"), "ArticleName");
            using (HttpClient httpClient = new HttpClient())
            {
                HttpResponseMessage response = await httpClient.PostAsync(@"https://localhost:44393/Articles/Create", formDataContent);
                return new ContentResult
                {
                    ContentType = "text/html",
                    StatusCode = (int)response.StatusCode,
                    Content = await response.Content.ReadAsStringAsync()
                };

            }
        }
    }

Note

Change the HttpClient part to send the right request to your own third party url with validate parameters.

Edward
  • 28,296
  • 11
  • 76
  • 121
  • Hi Tao Zho, thanks for your reply but the Requirement is: POSTing data while redirecting to a third-party payment gateway URL using Response.Redirect() then that third-party URL will be open a web page in that page user has to enter theire credentials for payment purpose. – Alexandar Rajavel Sep 12 '18 at 09:10