开发者

redirect to an external url on form submit

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜