我有一个winform应用程序,它是来自用户系统的集合信息,并将该数据传递给我的MVC应用程序。我的winform发布了这样的数据:
public void postTheData(string theData)
{
string dataToSend = "ThePostedData=" + theData;
var dataBytes = System.Text.Encoding.UTF8.GetBytes(dataToSend);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://localhost:52212/Licensing");
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = dataBytes.Length;
req.Method = "POST";
using (var stream = req.GetRequestStream())
{
stream.Write(dataBytes, 0, dataBytes.Length);
}
// -- execute request and get response
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{MessageBox.Show("Posted!");}
// Console.WriteLine("Hooray!");
}
我的MVC应用程序有一个名为“LicensingController”的控制器,我有:
public class LicensingController : Controller
{
public ActionResult Index()
{
//HandlePostedData();
return View();
}
[HttpPost]
public void ProcessLicensing(string thePostedData)
{
//ViewData["PostedData"] = "This Fired!";
System.IO.File.WriteAllText(Server.MapPath(@"~/App_Data/file.txt"), thePostedData + DateTime.Now.ToString());
//return View();
}
据我所知,HTTP帖子显示正确,我得到HTTPStatusCode.OK响应。但file.txt永远不会被写入。
这是我第一次涉足ASP.Net MVC,而我无法理解的是POST操作如何“连接”到“localhost / Licensing /”的控制器的“ProcessLicensing”方法? ?换句话说,如果有意义的话,我不知道如何将我发布的数据导向我需要的特定方法。
如果我创建了一个视图,我拥有“许可”:
<% using (Html.BeginForm("ProcessLicensing", "Licensing", FormMethod.Post))
{ %>
<%= Html.TextBox("ThePostedData", "DemoData") %>
Click to do a post back <input type="submit" id="submit" value="submit" />
<%} %>
这会将“演示数据”写入文本文件(带日期和时间),所以看起来问题出现在“表单名称”中???我不知道......我迷路了。任何帮助表示赞赏。
答案 0 :(得分:1)
您发布到索引页面:@"http://localhost:52212/Licensing"
您应该发布到@"http://localhost:52212/Licensing/ProcessLicensing"