Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I POST data from an asp.net MVC controller to a non-MVC asp.net page?

One department in our company is using classic asp.net while ours department is using MVC.

We need to pass 5 variables to his page (by form submit). Can someone please show a simple example of posting form data from an MVC controller to an asp.net page expecting the form variable?

like image 721
Monkey Avatar asked Jan 06 '11 04:01

Monkey


People also ask

How can post data to another page in asp net?

To post an ASP.NET Web page to another pageAdd a button control to your Web page, such as a Button, LinkButton, or ImageButton control. Set the PostBackUrl property for the control to the URL of the page to which you want to post the ASP.NET Web page.

How do you pass data from controller action to view pages?

ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary.

How pass data from controller model in ASP.NET MVC?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.


1 Answers

If I am reading this correctly, you should be able to do this without any cross domain / application concern. You want to do this in the controller, so you can use the HttpWebRequest class to post the data. It's conceptually the same as posting from a web browser as far as the target application is concerned.

Here's a quick and dirty snippet:

// name / value pairs. field names should match form elements
string data = field2Name + "=" + field1Value + "&" + field2Name+ "=" + field2Value

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(<url to other applications form action>);

// set post headers
request.Method = "POST";
request.KeepAlive = true;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";

// write the data to the request stream         
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);

// iirc this actually triggers the post
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
like image 83
Bill N Avatar answered Oct 20 '22 12:10

Bill N