简体   繁体   中英

Reusing MVC4 Controller for posting data from a desktop application

I have built an MVC4 web app with MongoDB as the backend. Some of the data that goes into Mongo originates from a desktop app and is posted via a WCF Service.

Was hoping to dump the WCF Service and just use the MVC4 controllers. It worked for the Get, but when I tried to Post, it produced an error.

I am certain there is plenty I am not aware of. I guess the first question is... is this possible?


Thank you MarcusTzaen: Yes WebAPI is the way to go. After a bit more testing I know why I was questioning.

From a WPF app the following request is sent (And Works): From a WPF app the following request is sent:

POST http://xxx.xxx.xxx/xxx/PostDocument HTTP/1.1
Content-Type: application/json
Host: xxx.xxx.xxx
Content-Length: 116
Expect: 100-continue
Connection: Keep-Alive

{ "id" : "2134", "vals" : { "some" : "value" }, "tia" : { "tsome" : "value" }, "specCond" : { "ssome" : "value" } }

The Web method receives the request as follows:

[System.Web.Mvc.ActionName("PostDocument")]
[System.Web.Mvc.HttpPost, System.Web.Mvc.HttpPut]
public Dictionary<string, object> PostDocument(object json)
{
     //do stuff here

      return dict;
}

When I do this from Microsoft Word I create the following request:

POST http://xxx.xxx.xxx/xxx/PostDocument HTTP/1.1 Content-Type: application/json Host: xxx.xxx.xxx Content-Length: 1223 Expect: 100-continue

{ "_id" : ObjectId("52b47bf88d1971193090155d"), "clientId" : "14173", "cdi" : "81098", "d" : "144", "dnd" : "12000", "dndi" : "1JB", "specCond" : { }, "vals" : { }, "tia" : { "tiaCount" : 0 } }

The difference ended up being the ObjectId, which the PostDocument web method choked on. Converting the ObjectId to a string solved the problem.

Why don't you use Web API ?

You can Specify that an action supports the POST HTTP method by using HttpPost .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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