Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Redirect and POST in ASP.NET MVC

0.00/5 (No votes)
25 Oct 2014 1  
An article that shows a simple workaround on how to make a redirect and POST in ASP.NET MVC

Introduction

About 5 years ago I wrote an article here in codeproject Redirect and POST in ASP.NET and it was successful and helpful for too many people, the article explained a simple solution on how to do redirect (point user browser to another page specially external URL - outside your application) and POST (send data to destination URL using http POST method) in ASP.NET technology (more specific and detailing: from the server side not client side), and as years go by and technologies arise the same need is required for ASP.NET MVC, I have been asked this question over and over again lately (Only lately and not sure why not earlier) on how to solve the same problem for ASP.NET MVC.

Of course I would recommend you to read that article for more explaination on the matter, because it will give pretty much detailed insight on why do I have all these solutions, of course thats up to you to read.

Background

The whole idea of redirect and post in ASP.NET was simple and neat: in code behind (server side) whenever we want to do a redirect and post we simply create an html form with http POST method and the url is set to destination url, we create a hidden field for each value we want it to be posted to destination url and we create a simple script block that will run once the whole html page is inturpretted, and the whole thing is returned in the response so it can be ran later by the browser of which it will do redirect and post. 

Using the Code

In ASP.NET MVC nothing changed, why? because we are still using http protocol and basically the Html technology, so same Idea is implemented for ASP.NET MVC except the way we return the html response is different in ASP.NET MVC and NOT BY MUCH, I just encapsulated the whole thing in an ActionResult implemented class I called RedirectAndPostActionResult, while in ASP.NET it was in a helper method takes Page instance as a parameter :)

The only difference from my side is that this time I added this peice of code to a library of mine called Fluentx.Mvc and not as a cs file you can download, because I already have many code snippets and classes that I wanted to share with others and I wanted to maintain all as an open source projects, this library is available on Nuget and its an extension to my main library Fluentx, just go to nuget and search for Fluentx and you will get both of them, download Fluentx.Mvc (it has a dependency on Fluentx) and you are good to go.

Now to the action: to do redirect and post for ASP.NET MVC first download Fluentx.Mvc and reference the namespance Fluentx.Mvc in your controller, secondly add the code below (similar of course) to where it fits your application:

public class YourController: Controller
{
    public ActionResult YourAction()
    {
        ... 
        ...
        ...
        Dictionary<string, object> postData = new Dictionary<string, object>();
        postData.Add("first", "someValueOne");
        postData.Add("second", "someValueTwo");
 
        return this.RedirectAndPost("http://TheUrlToPostDataTo", postData);
        //Or return new RedirectAndPostActionResult("http://TheUrlToPostDataTo", postData);
    }
}

Thats it!!!

Just note that the below method (RedirectAndPost) is an extension method, so make sure you have the reference to Fluentx.Mvc namespace in your c# file.

        return this.RedirectAndPost("http://TheUrlToPostDataTo", postData);

Points of Intreset

Nothing much to mention except the limitations of this solution as I have already mentioned this in my other article and I am qouting my self 5 years later: 

Quote:

If I am asked what the drawbacks of this solution are, I would say maybe one drawback ... which is that if the client is disabling JavaScript on the browser ... then this wouldn't work

Extra: JSON Related Post Data and Binding to View Model

I figured that I should mention this as I expect it to be needed by some of you: Posting JSON object to destination Url as POST, sometimes you might require that you need to do redirect and post of JSON object to a destination Url and the destination is an MVC controller action that takes a structured view model as a parameter to the action and you want it to be binded automatically to it.

Will without much explainations, I have an article in this subject specifically Redirect and POST JSON object in ASP.NET MVC, and in that article I am explaining everything, but wanted to mention that using this solution here combined with that one can make a great idea and solution: Post a JSON object from one controller action to another controller action and bind the received JSON object (in destination action) to a parametrized bounded view model.

Final thought

I aslo recommend you to have a look at Fluentx project, its a small project of mine that has a lot of helper classes and extension methods plus a great simple object to object mapper and many many other stuff (c# control statements, specification pattern for internal business rules ...), the library is very rich and very helpful for .NET developers.

History

Article created October 26th, 2014

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here