我们公司的一个部门正在使用经典的 asp.net,而我们的部门正在使用 MVC。
我们需要将 5 个变量传递到他的页面(通过表单提交)。有人可以展示一个简单的示例,将表单数据从 MVC Controller 发布到需要表单变量的 asp.net 页面吗?
最佳答案
如果我没看错的话,您应该能够在没有任何跨域/应用程序问题的情况下执行此操作。你想在 Controller 中执行此操作,因此你可以使用 HttpWebRequest发布数据的类。就目标应用程序而言,它在概念上与从 Web 浏览器发帖相同。
这是一个快速而肮脏的片段:
// 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();
关于c# - 如何将数据从 asp.net MVC Controller 发布到非 MVC asp.net 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4611655/