Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Service or web API for connecting a windows application to MVC 4?

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..

like image 926
Bram van den Besselaar Avatar asked Dec 10 '12 15:12

Bram van den Besselaar


Video Answer


2 Answers

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

like image 108
Nirmal Avatar answered Sep 28 '22 10:09

Nirmal


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.

like image 22
SkSirius Avatar answered Sep 28 '22 11:09

SkSirius