Click here to Skip to main content
13,139,569 members (53,530 online)
Click here to Skip to main content
Add your own
alternative version

Stats

51.6K views
987 downloads
21 bookmarked
Posted 25 Oct 2014

Redirect and POST in ASP.NET MVC

, 25 Oct 2014
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Samer Abu Rabie
Engineer C4 Advanced Solutions
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.

You may also be interested in...

Pro

Comments and Discussions

 
QuestionCan use this solution for auto login Pin
Member 1233619229-May-17 13:47
memberMember 1233619229-May-17 13:47 
AnswerRe: Can use this solution for auto login Pin
Member 1337871825-Aug-17 14:02
memberMember 1337871825-Aug-17 14:02 
QuestionImpossible to find Fluentx .mvc or Fluentx in NuGet Pin
Member 1233588018-Feb-16 0:25
memberMember 1233588018-Feb-16 0:25 
AnswerRe: Impossible to find Fluentx .mvc or Fluentx in NuGet Pin
Samer Abu Rabie7-Jul-16 1:09
professionalSamer Abu Rabie7-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
memberniravnayak8-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 26 Oct 2014
Article Copyright 2014 by Samer Abu Rabie
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid