Web API client organization - c#

I have a c# web API project that exposes a lot of methods through HTTP and I need to consume it in a c# web mvc application. I use the HttpClient class for this but my question is, how should I organize the access to this web API? for example, the web api project has n controllers, so I could create n IClient interfaces, one per API controller, in the mvc site, or just one big IClient that contains a method for every single API method exposed in the web api project? Is there a preferred approach for this? (that doesn't involve auto generated client code)

Related

Call methods of a separated class library project by using Ajax

Is it possible to create a separated class library project as the web service of an ASP.NET MVC project? (Actually, I want to have an MVC Project and a class library project as its web service to call methods by using Ajax in the web project)
I tried this as the URL of ajax request but Ajax couldn't find it: ('Mine' and 'Service' are namespaces; 'UserService' is the name of the class, and the 'SignUpUser' is the method I want to call:
url: "Mine.Service.UserService/SignUpUser"
There is a much better way to call your web service.
You can use the WEB API in ASP.NET to expose your services and data rather than creating separated class library to call your web service

When Web API is called from same project is working fine. But when I call same API from another project in same machine, it is giving 405 method

First time when I created Web API project(MVC pattern) and I created few API controllers and called from html page(Ajax call),I am able to call all APIs and get the data.
But second time I've created two separate solutions one is asp.net Web API and another is MVC Web application.
Now I'm running both the projects in my machine and trying to call web APIs from MVC application, it's coming 405 method not allowed client side error.
Can anyone help me with this scenario.
Anything extra I need to configure in my server Web API to be called from different application?

Asp.net Web Api Configuration

I am new to asp.net mvc world. So I have a question
I have already developed a web app using Asp.net MVC (also deployed on production).
Currently I am working on mobile apps. For this I need web services (restful).
For Restful web services do I have to make a new project (within existing solution) or can I incorporate Restful webservices into my existing (Asp.net MVC) project ? (I prefer 2nd option if possible)
If I have to make new project for Web Api, then how will I deploy both projects on production knowing that Web Api project is dependent on Asp.net-MVC project ?
One thing you need to understand first is whether it's a web service,wcf service or a Web API the only thing you need is to get a json/xml output which you can use in your mobile app.
Let say you have and asp.net mvc application which has some action methods, but you might be returning a View or PartialView which is not you want for a mobile app to parse. So you need to create an action method which returns JsonResult.
If you want to use all the RESTFul verbs like POST,PUT,GET,DELETE you can add another controller which inherits from APIController and write methods there, but either ways output is same.
So it's up to you what to do and how to proceed, only thing is with an APIController you will have some more verbs and code ahve some special returns like "Ok" e.t.c

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..
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.
Given the tags you've used, my guess is that you're deciding between SOAP Web Services and WCF. Given these two, I say to go WCF. SOAP web services (as implemented in Visual Studio) are the older technology; still serviceable, but WCF can do everything an older SOAP service can do (including look exactly like a SOAP service) and more.
If you have a web service that connects your web server to your database server (these two things should be on different machines; your web server is exposed to the world by necessity, while your DB server should be locked down like Fort Knox), I see no reason why you shouldn't use that same service as-is for an internal WinForms application (using a LAN/VPN to access the service layer on the DB server). For a WinForms application that must access the data over the Internet, I would recommend reimplementing the service as a WCF service supporting secure encrypted data transfer. You can also set up the service endpoint to only accept HTTPS connections, and thus simply run your existing service through SSL/TLS.
What you choose will primarily depend on how much time-resources you can commit to resolving the problem; moving to HTTPS is a fast fix requiring little if any code changes, while reimplementing in WCF will take more time but will allow additional security measures beyond a simple secure tunnel.
Or something lightweight like Nancy: http://nancyfx.org/
We had some issues with MVC4 WebApi stuff and ended up using ServiceStack on the server side JavaScript/AJAX for web clients and RestSharp for thick clients.
One of our specific issues was the inability to auto generate documentation, significant performance differences, and better support for unit/integration testing.
Rather than advocate specifically WCF, I'd recommend WCF Data Services or OData, with the stipulation that you'll need to secure it. If you go for pure WCF, you'll see that you'll end up creating a lot of code to handle retrieving the info from a database, and then sending that information right back out to your clients. It doesn't sound that bad, at first, but after about 30 entities in a database, you'll quickly grow tired of a pure WCF solution.
OData is great, it uses Entity Framework, and it quickly opens data manipulation for an existing database or one you are going to make. It will save you a ton of development time, if you can make your service secure. The format of the data response is flexible. There are plenty of client libraries ported for other programming languages as well.
The steps for securing a service are pretty simple. Always deploy to https. Any login or registration methods , need to be post methods, that return a token (Encrypted value), or a unique secret that can be encrypted and sent back for any subsequent requests. It's better to use the token, and have an expiration on the token.. because otherwise both your service and your app whether mobile or desktop, need to have a shared encryption / decryption method.

C# program to call existing web API

How can I call / use web API that supports SOAP and REST? I want to write a program in C# that will use this web API. I didn't get much information on the internet. So far, I have created C# project and added the web service reference (WSDL) to my program. Now how can I use the web API. How to send request or receive response? Can you please refer me some good tutorials ?
The HttpClient class is a good starting point. Also there is the EasyHttp library.
Code sample HttpClient: http://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee
Introduction to EasyHttp: http://devlicio.us/blogs/hadi_hariri/archive/2011/01/16/easyhttp.aspx
Adding web service reference creates client classes that can be used to communicate with web service. All created classes will be in new namespace, You have to look for client proxy class (full name depends on WebService name). Create instance of this class. Calling its methods will call WebService. Here you can read about using Web Services in C#

Categories