Metaphor
Metaphor

Reputation: 6415

Using Web API in an MVC Model

I have a Web API that is currently being used in java script clients and I would like to use some of the APIs in MVC projects as the data source.

I haven't been able to find information on consuming the Web API from an MVC Class. Maybe I'm just not looking in the right places. Is there a .Net method for doing this?

Upvotes: 1

Views: 174

Answers (2)

Dan Esparza
Dan Esparza

Reputation: 28385

If you want an easy way to POST and GET from RESTful urls (like Web API) check out ServiceStack.Text

You can do operations like:

var resultString = fullUrl.PostJsonToUrl(args);
MyType results = resultString.FromJson<MyType>();

Where ...

args can be any object (including an anonymous type) and it gets serialized to JSON automatically.

fullUrl is just a string with the url you're posting to / getting from.

MyType is a class with properties that you're deserializing to. It needs to match the JSON spec that you're getting back.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161821

If you are using a recent version of .NET, then you should simply use the HttpClient class.

Upvotes: 2

Related Questions