1

I am usig Asp.net core 3.1 and I must to redirect user to a url with Post Action, what should I do? is it possible?

public async Task<IActionResult> RedirectTo() { // do something to generate a url // redirectUrl = "https://xx.com/?token=zzzzz" redirect and Post to redirectUrl //how ??????????????????? } 
2
  • 4
    You can't redirect to a POST method. What exactly are you thinking to POST here anyway? Commented Feb 22, 2021 at 12:15
  • I want to direct the user to the bank portal. According to the bank document, I have to post 'redirectUrl' to the bank portal. I actually want to navigate user to this path by post action. is it possible???!?! Commented Feb 22, 2021 at 12:26

1 Answer 1

4

Check here for more information as to why this is not supported

You can't really do it BUT, there is a clever workaround here

 HttpResponse response = HttpContext.Current.Response; response.Clear(); StringBuilder s = new StringBuilder(); s.Append("<html>"); s.AppendFormat("<body onload='document.forms[\"form\"].submit()'>"); s.AppendFormat("<form name='form' action='{0}' method='post'>", url); foreach (string key in data) { s.AppendFormat("<input type='hidden' name='{0}' value='{1}' />", key, data[key]); } s.Append("</form></body></html>"); response.Write(s.ToString()); response.End(); 

What this does, is create on html response with a form that will post a request to the required page with javascript.

You need to be in the same domain for this to work. If you redirect, then you can only do so with GET - parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, But can you write your code clear or describe more? because I dont know where and how use your trick. thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.