After searching the entire day about what I should use, I'm not sure what option would be best for my needs so I hope someone with more experience could help me out.
I have a winforms
application (c#
) and a ASP.NET MVC 4
web application (c#
).
I wish to connect these, the goal is to send and receive data from the database which I use in the MVC 4
project, but from within the windows forms application. The data I send from the windows forms application to the database, is then used by the MVC 4
web application.
I am entirely new to web services / Web Api
's so I can't really decide what option would be best. Any help would be much appreciated..
If you already created MVC4
project then you can add actions to any controller
and return JSON
data like below :
public JsonResult GetCategoryList()
{
var list = //return list
return Json(list, JsonRequestBehavior.AllowGet);
}
or you can create new project of MVC4
and select WEBAPI
template . It will create webapi project for you .It will create with example .so it will easy for to create webapi
.In webapi
it return data automatically convert to xml
and json
as per request
The WCF
Web API abstractions map to ASP.NET
Web API
roughly as follows
WCF Web AP -> ASP.NET Web API
Service -> Web API controller
Operation -> Action
Service contract -> Not applicable
Endpoint -> Not applicable
URI templates -> ASP.NET Routing
Message handlers -> Same
Formatters -> Same
Operation handlers -> Filters, model binders
Other Links
If you have an MVC 4 App already, it would be better to use Web API (RESTful service) I assume you have some knowledge in building REST API (understanding of POST, PUT, UPDATE stuff)
It is simple in configuration and usage. All what you need actually is to create a new controller like:
class MyApiController: ApiController {
public Post(SomeClass item) {
....connect to db and do whatever you need with the data
}
}
You'll also should configure routing for Api.
And then in your winForms app you can simply use HttpClient
class to perform api call.
HttpClient aClient = new HttpClient();
// Uri is where we are posting to:
Uri theUri = new Uri("https://mysite.com/api/MyApi");
// use the Http client to POST some content ( ‘theContent’ not yet defined).
aClient.PostAsync(theUri, new SomeClass());
Take a look at some implementation details right here: Web Api Getting Started
Get started with WCF is not so easy as with Web API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With