1

Is there any way to redirect to a controller and pass along a JSON object as a POST method? I am able to achieve this with GET method using Response.Redirect("../ControllerName/ActionName?parameter=" + jsonQuery, true); but then GET methods have a limit on query string length. After certain length it will throw an exception.

Also any alternative methods would be very helpful. What I am trying to achieve is the following.

I have an application that WebForms and MVC runs side by side. I have a page on webforms and a report button on that page. When Report button is clicked I need to put together some data on the code behind (aspx.cs file) and pass it to my controller and from there I will populate my model and pass it to my view.

I am open to any suggestions and any alternative methods.

Balash
  • 143
  • 1
  • 2
  • 8
  • Are you familiar with the System.NetHttpWebRequest class? You can perform a post via code to any URL. – Tasos K. Nov 05 '13 at 20:23
  • I have actually never used that class. I will look into it and hopefully it will do the trick :). Thank you so much. – Balash Nov 05 '13 at 21:24
  • @codingstill Looks like HttpWebRequest class can be used for posting an information but problem that occur with my situation is that I need the post to occur during the redirect process. So I need to be able to go into a controller with posted data and render the view from there. Any suggestions? THanks. – Balash Nov 05 '13 at 22:04
  • I suggest you to post directly to the controller on the MVC site and perform whatever you do in the code-behind in the controller. If you don't want to repeat the code, you can put the shared code in a DLL used by both projects. – Eduardo Molteni Nov 05 '13 at 22:10
  • Could [Server.Transfer](http://msdn.microsoft.com/en-us/library/ms525800(v=vs.90).aspx) meet your needs? – Snixtor Nov 05 '13 at 22:23
  • @Balash you are correct, the HttpWebRequest won't redirect the browser. I would think an approach using JavaScript or jQuery. Take a look here, you might find it helpful http://stackoverflow.com/questions/4583703/jquery-post-request-not-ajax. – Tasos K. Nov 05 '13 at 22:23

2 Answers2

0

You cannot easily redirect to a post - a redirect sends back the new URL to the client, which in turn will get the new URL.

You can redirect to a special form which self-posts (you dump the post parameters onto the page, then javascript runs when the page is loaded which submits a completed form to your final action). Keep in mind that doing it this way (assuming you're not using AJAX) means you'd be submitting regular form-encoded parameters, so you'd have to do like you have in your example, with your JSON inside of a single input tag, something like:

<input id="myparam" type="hidden" name="myparam">

var someJsonString = "{---this string is built on the server---}";
document.getElementById("myparam").value = someJsonString;

If you post to your controller action yourself using HttpClient or WebClient or similar methods, you'd be posting as your server making an outgoing connection, rather than from the browser, meaning the IP would be your server's outgoing IP, you wouldn't have any cookies, authentication, etc., from the browser.

There are ways to cross between WebForms and MVC, but last time I looked, it was not an easy task - a lot of custom stuff involved to turn a regular context into a usable MVC context.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • [This question](http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get) discusses the redirect in much more detail. – Joe Enos Nov 05 '13 at 22:01
  • Thats a great info Joe. Thank you so much. Yeah its been a tough task to figure out how to redirect from WebForms to Controller. I am basically looking for RedirectToAction equivalent that can redirect with data from webforms to controller. – Balash Nov 05 '13 at 22:25
  • Keep in mind that even RedirectToAction doesn't re-post a form - it does the same thing as `Response.Redirect` - it delivers a 302 back to the browser, which in turn does a `get` request on the new URL. – Joe Enos Nov 05 '13 at 22:42
  • Well I wouldn't mind using GET request but the problem is that my JSON string can get large enough that it throws query string length exception. Is there any way to bypass that maybe? – Balash Nov 05 '13 at 22:45
  • I know the `GET` limit is different per browser - I tend to lean toward rather-be-safe-than-sorry, so I'd consider 255 to be the upper bound, which means I wouldn't even try to do it. The answer I link to above gives a few discussion points - you can try a 307 redirect if you want, but I'm confident in Pavlo's answer - I've seen that work in several enterprise-quality solutions. The only thing you really have to be careful with is how you escape out your JSON, depending on whether you put it in a javascript line, or directly in the HTML, or in a separate script tag, etc. – Joe Enos Nov 05 '13 at 22:53
-1

This looks like what you're looking for...

.net post form in code behind

String ali = "your string";

            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(ali);

            WebRequest req = WebRequest.Create("http://www.pay-pos.com/test");

            req.ContentType = "application/x-www-form-urlencoded"; 
            req.ContentLength = byteArray.Length;
            req.Method = "POST"; 

            Stream dataStream = req.GetRequestStream(); 
            dataStream.Write(byteArray, 0, byteArray.Length); 
            dataStream.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            String responseString = reader.ReadToEnd();

            response.Close();
Community
  • 1
  • 1
tintyethan
  • 1,772
  • 3
  • 20
  • 44
  • Ethan WebRequest can be used to POST and get a response but what I am looking for is to pass the data with redirect. I want to take the flow from webforms to a controller which will in turn return a view. – Balash Nov 05 '13 at 22:30