c# - 如何将数据从 asp.net MVC Controller 发布到非 MVC asp.net 页面?

标签 c# asp.net-mvc-2 post get

我们公司的一个部门正在使用经典的 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/

相关文章:

c# - RSA加解密结果有3个问号

c# - POST参数中的asp.net mvc周期

使用 HttpURLConnection 的 Android POST 请求

c# - 在 C# 中取消另一个任务时运行一个任务

c# - 为什么 Unity Event 以错误的方式工作?还是我?

javascript - 如何使用 jquery 禁用列表框中的多项选择?还是javascript?

asp.net - 如何获取 Telerik MVC 网格数据

java - Android 应用程序在访问本地主机时崩溃

json - 如何在cakephp中发送json帖子

c# - 创建 TFS 自定义 checkin 策略 : Check Comment if it contains certain string