Api redirect sending Post values

diego gutierrez 161 Reputation points
2022-01-14T19:36:28.017+00:00

Hi everybody.

How can I do to redirect sending post values?

If I use RedirectPermanent it doesn't have parameters to send an object. I can only redirect a url using get but I don't know how to send post parameters.

 [HttpPost]
        public ActionResult<string> Post([FromForm] ObjectDataIn objeto)
        {

                var objRtsRequest = (string)HttpContext.Items["objRtsRequest"];
                //return RedirectToPagePermanent("https://toolsdev.avtest.ink/test", objRtsRequest); //TEST 1
                return RedirectPermanent("https://toolsdev.avtest.ink/test"); //TEST 2

        }

Thanks in advance.

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2022-01-14T19:47:38.817+00:00

    A redirect is a GET request, by design. What gets sent back to the browser is the URL to redirect to. The browser then triggers a GET request to that URL. GET requests cannot have a body and hence only the URL works. This is why the PRG (Post-Redirect-Get) process is recommended for updates like yours.

    In your specific case it appears you are redirecting to another site so you just need to build up the correct URL. If this is a redirection within your own site then use RedirectToAction or equivalent and pass the extra data as part of it so the properly URL can be generated.

       return RedirectPermanent("https://toolsdev.avtest.ink/test?id=10&somevalue=Bob");  
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-01-14T20:40:07.317+00:00

    Code accesses Web API actions not a browser. The redirect does not make a lot of sense for a Web API action. Typically a URL is returned to caller and the caller invokes the redirect. For example, if the caller is AJAX then you would do a location.href = "https://www.google.com/" in the response callback.

    If you are trying to invoke a remote action the use HttpClient to submit a POST to the URL.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.