简体   繁体   English

How to Redirect and post a collection of data to destination URL and redirect to that external URL in MVC c# without using Ajax or client side

[英]How to Redirect and post a collection of data to destination URL and redirect to that external URL in MVC c# without using Ajax or client side

I need to post data to third party URL and redirect to that URL on submit click.我需要将数据发布到第三方 URL 并在提交点击时重定向到该 URL。 After clicking submit I need to do some business logic and post the form data to the third party URL and redirect to that URL.单击提交后,我需要执行一些业务逻辑并将表单数据发布到第三方 URL 并重定向到该 URL。

I tried using Submit button with action to controller action method, but is there any way like response.redirect, passing collection of data to be submitted to the destination URL and then all data is posted with redirection.我尝试使用带有动作的提交按钮 controller 动作方法,但是有没有像 response.redirect 这样的方法,将要提交的数据集合传递到目标 URL,然后所有数据都通过重定向发布。

Thank you for your help and response in advance感谢您的帮助和提前回复

If the third party URL is a get method and accepts the same parameter as the POST then you can redirect the user and send the data to the third party site at the same time with a single redirect action.如果第三方 URL 是一个 get 方法并接受与 POST 相同的参数,那么您可以通过单个重定向操作将用户重定向并将数据同时发送到第三方站点。 For example:例如:

var model = new MyObject { Id =100, Name = "ABC", Code = "100ABC"};
return RedirectToAction("Details","Store", model);

This code will send a 302 response to the browser with query string in Url as此代码将向浏览器发送一个 302 响应,其中 Url 中的查询字符串为

/url?Id=100&Name=ABC&Code=100ABC

Here the MyObject will be the posted object to your action.这里的 MyObject 将是发布到您的操作的 object。

HTTP POST and redirect work in a different way. HTTP POST 和重定向以不同的方式工作。 If you are doing POST from back end then after the post redirect the user to the URL from the back-end too.如果您正在从后端进行 POST,那么在发布后也将用户从后端重定向到 URL。 In that case there would be a post first then redirection.在这种情况下,首先会有一个帖子,然后是重定向。 I think you are not looking for this one.我想你不是在找这个。

Thank you for all your help, I was able to post the form data to external URL by creating a form with all required data in controller, return that form through Ajax to js file and through jQuery form submit and its working fine. Thank you for all your help, I was able to post the form data to external URL by creating a form with all required data in controller, return that form through Ajax to js file and through jQuery form submit and its working fine.

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

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