Create controller to handle Person class data .
public class PersonsController : System.Web.Http.ApiController
{
static Person[] s_persons = new Person[]
{
new Person() { Name = "John", Height=180},
new Person() { Name = "Paul", Height=170}
};
public IEnumerable<Person> Get()
{
return s_persons.ToArray();
}
public Person Get(int id)
{
return s_persons.Where(p => p.Id == id).FirstOrDefault();
}
public System.Net.Http.HttpResponseMessage Post(Person newPerson)
{
s_persons.Add(newPerson);
// pass back to the caller success and new id (if database is used)
System.Net.Http.HttpResponseMessage result = new System.Net.Http.HttpResponseMessage(HttpStatusCode.OK);
result.Headers.Location = new Uri("http://localhost:34460/api/persons/" + Convert.ToString(newPerson.Id)); // Id field would be autogenerated if database is used
return result;
}
public void Put(Person personArg)
{
Person person = m_persons.Where(p => p.Id == personArg.Id).FirstOrDefault();
person = personArg;
}
public void Delete(int id)
{
Person person = m_persons.Where(p => p.Id == id).FirstOrDefault();
s_persons.Remove(person);
}
}
More info and samples on: www.devarchweb.net
Define routing (to api/persons) Create App_Start folder.using System.Web.Http;
namespace WebApiSimple
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
<%@ Application Codebehind="Global.asax.cs" Inherits="WebApiSimple.WebApiApplication" Language="C#" %>
namespace WebApiSimple
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
WebApiConfig.Register (System.Web.Http.GlobalConfiguration.Configuration);
}
}
}
More info and samples on: www.devarchweb.net
How looks URL for service providing people data (all people, person with id 1) type: http://myserver/api/persons to get list of personsMore info and samples on: www.devarchweb.net
How to call web method and display all people with JavaScript/jQuery<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script>
function listAllPeople()
{
$.getJSON('api/persons').done(
function (people)
{
$.each(people, function (key, person)
{
$('<li>', { text: person.Id + " " + person.Name } ).appendTo( $('#persons') );
});
}
);
}
<script>
More info and samples on: www.devarchweb.net
How to access web service from .net client with WebRequest - Create a console appHttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://localhost:34460/api/persons");
myHttpWebRequest.Method = "GET";
myHttpWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
myHttpWebRequest.ContentType = "application/json";
myHttpWebRequest.Accept = "application/json";
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
// data in JSON format
System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myHttpWebResponse.GetResponseStream());
string responseData = myStreamReader.ReadToEnd();
// deserialize the data
var p1 = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Person[]>(responseData);
More info and samples on: www.devarchweb.net
How to access web service from .net client with HttpClient - Create console app static void Main()
{
string baseAddress = "http://localhost:34460/";
CallWebService(baseAddress + "api/persons").Wait();
}
static async Task CallWebService(string baseAddress)
{
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true; // needed for Windows authentication
using (HttpClient client = new System.Net.Http.HttpClient(handler))
{
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// get all items
HttpResponseMessage httpResponseMsg = await client.GetAsync("");
if (httpResponseMsg.IsSuccessStatusCode)
{
Person[] person = await httpResponseMsg.Content.ReadAsAsync<Person[]>();
}
// get one item
httpResponseMsg = await client.GetAsync(baseAddress+"/1");
if (httpResponseMsg.IsSuccessStatusCode)
{
Person person = await httpResponseMsg.Content.ReadAsAsync<Person>();
}
// add new - Post
Person newPerson = new Person() { Id=3, Name = "George" };
httpResponseMsg = await client.PostAsJsonAsync("", newPerson);
if (!httpResponseMsg.IsSuccessStatusCode) { return; }
Uri newlyAddedPersonUrl = httpResponseMsg.Headers.Location;
// update existing
newPerson.Name = "Ringo";
await client.PutAsJsonAsync(newlyAddedPersonUrl, newPerson);
// delete existing
await client.DeleteAsync(newlyAddedPersonUrl);
}
}
More info and samples on: www.devarchweb.net
How to receive data in XMLclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
More info and samples on: www.devarchweb.net
How to use Fiddler to make POST web service calls -Navigate to Composer tabMore info and samples on: www.devarchweb.net
How to route methods with attributes[HttpGet]
public IEnumerable<Person> RetrieveAll() {... }
[HttpPost]
public System.Net.Http.HttpResponseMessage Add(Person newPerson) {...}
[HttpPut]
public void Update(Person personArg) { ... }
[HttpDelete]
public void Remove(int id) { ... }
More info and samples on: www.devarchweb.net
How to call delete as GET (from browser)// enables http://localhost:34460/apiaction/persons3/delete/1
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "apiaction/{controller}/{action}/{id}", // prefix "apiaction" is different in order to avoid conflict with api/persons/1 and api/persons/action
defaults: new { id = RouteParameter.Optional }
);
....
[HttpGet]
public void Delete(int id)
{
Person person = s_persons.Where(p => p.Id == id).FirstOrDefault();
s_persons.Remove(person);
}
More info and samples on: www.devarchweb.net
How to call multiple get methods in one controller (2 options)public Person GetOne(int id)
{
return s_persons.Where(p => p.Id == id).FirstOrDefault();
}
public Person GetByName(string name)
{
return s_persons.Where(p => p.Name == name).FirstOrDefault();
}
config.Routes.MapHttpRoute(
name: "ActionApi2",
routeTemplate: "apiaction2/{controller}/{action}/{name}",
defaults: new { id = RouteParameter.Optional }
);