开发者

redirect to an external url on form submit

开发者 https://www.devze.com 2023-01-22 06:28 出处:网络
i have a form which on submit should redirect to an external URL to perform some action with my formdata, as well as remain on the home page after successful submission. i used redirec开发者_StackOver

i have a form which on submit should redirect to an external URL to perform some action with my form data, as well as remain on the home page after successful submission. i used redirec开发者_StackOverflow社区tion, but this will make my second option possible, but not my first one. Please help..


You have different possibilities here. The first possibility is to set the action attribute of the form directly to the external url and add a returnurl hidden input parameter. When the form is submitted it will POST data to the external url to process and when it finishes processing the external url will use the returnurl parameter to redirect back to your home page.

Another possibility is to call the external url in your POST action using WebClient to send data for processing and return the same view:

[HttpPost]
public ActionResult Index(SomeViewModel model)
{
    using (var client = new WebClient())
    {
        var values = new NameValueCollection 
        {
            { "param1", model.Property1 },
            { "param2", model.Property2 },
        };
        // send values for processing to the external url
        var result = client.UploadValues("http://externalurl.com", values);
        // TODO: analyze result
    }
    return View(model);
}


You need to manually program for this. For example you can pass a returnUrl parameter (e.g. via the query string) to the second page and that page will be in charge of reading this parameter and perform a redirect of its own.

0

精彩评论

暂无评论...
验证码 换一张
取 消