Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET

Redirect and POST in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.93/5 (20 votes)
25 Oct 2014CPOL4 min read 125K   1.8K   21   11
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:

C#
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Jordan Jordan
Samer is a Computer and Digital Electronics Engineer who lives in Abu Dhabi UAE, worked on a fair number of personal projects as part of fun or freelancing, mostly related to integration between hardware and software (e.g Security Systems, Sensors, Cameras, Bluetooth ... etc), which gave him a powerful knowledge in this area and the ability to invent various complex designs to adhere for external elements effect those systems.

Through his career path he worked on a fair number of enterprise projects with well known software companies, which showed him the various aspects of an enterprise applications.

You may contact Samer through his e-mail: SamerX@outlook.com or Twitter: SamerX as well.

Comments and Discussions

 
PraisePerfect Module Pin
Danial Pato8-Jun-19 4:15
Danial Pato8-Jun-19 4:15 
QuestionIs there anyway we could do this without using JS? Pin
santhudr29-Nov-18 5:58
santhudr29-Nov-18 5:58 
QuestionSame issue I'm facing in ASP.Net Core 2.0 so please kindly help on this Pin
Member 1398187012-Sep-18 2:02
Member 1398187012-Sep-18 2:02 
AnswerRe: Same issue I'm facing in ASP.Net Core 2.0 so please kindly help on this Pin
JRyomaru20-Jun-19 23:42
JRyomaru20-Jun-19 23:42 
QuestionHow to open in new window Pin
Member 138583949-Jul-18 11:25
Member 138583949-Jul-18 11:25 
QuestionCan use this solution for auto login Pin
Member 1233619229-May-17 13:47
Member 1233619229-May-17 13:47 
AnswerRe: Can use this solution for auto login Pin
Member 1337871825-Aug-17 14:02
Member 1337871825-Aug-17 14:02 
QuestionImpossible to find Fluentx .mvc or Fluentx in NuGet Pin
Member 1233588018-Feb-16 0:25
Member 1233588018-Feb-16 0:25 
AnswerRe: Impossible to find Fluentx .mvc or Fluentx in NuGet Pin
Samer Aburabie7-Jul-16 1:09
Samer Aburabie7-Jul-16 1:09 
GeneralMy vote of 5 Pin
Giovanni Scerra21-Aug-15 11:11
professionalGiovanni Scerra21-Aug-15 11:11 
QuestionPost latest page viewmodel and reopen same page with some querystring par appended in new tab. Pin
niravnayak8-Dec-14 18:26
niravnayak8-Dec-14 18:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.