You must Sign In to post a response.
  • Category: ASP.NET

    How to call Web API method in ASP.NET Application


    I have created an Web API using MVC 4.0, i have created a class file and a controller for POST method in a Application, i want to call the an API POST method in APS.NET Web Application,
    so i need suggestion please help me out.

    Abhishek Patil
  • #766651
    [Response removed by Admin. Read forum policies.]
    Name : Dotnet Developer-2015
    Email Id

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766888 please find the videos that are present in the website.

  • #766920
    Method indicated by the [HttpPost] attribute is an HttpPost method.
    You just need to call PostAsJsonAsync() method of HttpClient i.e. HttpClientObject().PostAsJsonAsync("api/yourAPIname", arguments if any for Model Class);

    Please refer below mentioned links:

  • #766927
    You can easily access using simple HTTP call. Create instance of http client and do post call with the required parameter. If the web API is the REST type then you can make Http call by sending input(Json,Keyvalue etc..)
    Following is the sample code to access the web api

    using (var Myclient = new HttpClient())
    Myclient.BaseAddress = new Uri("http://localhost/AppName/");
    Myclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage Myresponse = await client.GetAsync("webapi/Items");
    if (Myresponse.IsSuccessStatusCode)
    Item MyItem = await response.Content.ReadAsAsync>Item>();

    By Nathan
    Direction is important than speed

  • #766949
    WebClient is a part in WebApi below the snippet of code will help you to know what is webAPI.

    string URI = @"http://XXX.XXX.XXX.XX/XXXXXX/index.php?fn=addSession";
    WebClient wc = new WebClient();
    NameValueCollection inputs = new NameValueCollection();
    inputs.Add("fn", "addSession");
    inputs.Add("id", sessionid);
    inputs.Add("step", stepid.ToString());
    inputs.Add("option", type);
    inputs.Add("date", datetimenow.ToString());
    inputs.Add("view", "0");
    inputs.Add("datefrom", fromdate.ToString());
    inputs.Add("dateto", todate);
    inputs.Add("numfrom", numfrom.ToString());
    inputs.Add("numto", numto.ToString());
    inputs.Add("word", EncryptionDecryption.Encrypt(keywords.ToString(), true));
    inputs.Add("phrase", EncryptionDecryption.Encrypt(phrases.ToString(), true));
    inputs.Add("other", otherfiledetails);
    byte[] retvalue = wc.UploadValues(URI, inputs);
    string value = ASCIIEncoding.ASCII.GetString(retvalue);


  • #766989

    try this piece of code

    Product[] products = new Product[]
    new Product { Id = 1, Name = "Mouse", Price = 300 },
    new Product { Id = 2, Name = "Optical Mouse", , Price = 450 }

    public IEnumerable<Product> GetAllProducts()
    return products;

    public IHttpActionResult GetProduct(int id)
    var product = products.FirstOrDefault((p) => p.Id == id);
    if (product == null)
    return NotFound();
    return Ok(product);

    var uri = 'api/products';

    $(document).ready(function () {
    // Send an AJAX request
    .done(function (data) {
    // On success, 'data' contains a list of products.
    $.each(data, function (key, item) {
    // Add a list item for the product.
    $('<li>', { text: formatItem(item) }).appendTo($('#products'));

    function formatItem(item) {
    return item.Name + ': $' + item.Price;

    function find() {
    var id = $('#prodId').val();
    $.getJSON(uri + '/' + id)
    .done(function (data) {
    .fail(function (jqXHR, textStatus, err) {
    $('#product').text('Error: ' + err);

    <h2>All Products</h2>
    <ul id="products" />
    <h2>Search by ID</h2>
    <input type="text" id="prodId" size="5" />
    <input type="button" value="Search" onclick="find();" />
    <p id="product" />

    Name : Dotnet Developer-2015
    Email Id

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

Sign In to post your comments