简体   繁体   English

如何使用ASP.Net MVC3设置简单的Web服务?

[英]How can I setup a simple web service using ASP.Net MVC3?

I have created a client-based winforms application that parses a file and creates a Game object. 我创建了一个基于客户端的winforms应用程序,该应用程序分析文件并创建Game对象。

The plan is to have that client send the Game object to a webservice that is expecting that object type. 计划是让客户端将Game对象发送到期望该对象类型的Web服务。 Then on the server side, I would use Entity-Framework 4 to persist that information. 然后在服务器端,我将使用实体框架4来保留该信息。

Essentially, that Game object is just a POCO/Model class. 本质上,该Game对象仅仅是POCO / Model类。

How would I set things up on the server side so my application exposes a service and furthermore expects that type of object. 我将如何在服务器端进行设置,以便我的应用程序公开服务并进一步期望该类型的对象。

Thank you. 谢谢。

*snip* Scratch that, it wasn't what I needed. * snip *抓取,这不是我所需要的。

You can take advantage of MVC's ModelBinder functionality to automatically compose your input models from form or query data, eg: 您可以利用MVC的ModelBinder功能自动从表单或查询数据组成输入模型,例如:

public ActionResult Save(Game game)
{
    int id = game.Id;
    // Do work
    ...

And then on your client: 然后在您的客户上:

var request = (HttpWebRequest)WebRequest.Create("http://localhost/Game/Save");
string data = string.Format("Id={0}&Name={1}", HttpUtility.UrlEncode(game.Id), HttpUtility.UrlEncode(game.Name));
request.Method = "POST";
request.ContentLength = data.Length;
request.contentType= "application/x-www-form-urlencoded";

using (var writer = new StreamWriter(request.GetRequestStream()))
{
  writer.Write(data);
}

// Post the data.
var response = (HttpWebResponse)request.GetResponse();

This is all overkill, there are technologies that already exist such as WCF services, etc which will handle the proxy generation and communication for you. 这全都是矫kill过正,已有诸如WCF服务之类的技术可以为您处理代理生成和通信。 Have you considered them? 你考虑过吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM