pooja Malik
pooja Malik

Reputation: 35

WebApi implementation

I am learning WebAP,I have worked on Webservices before.

I am confused in implementation of MVCWebApi.

My understanding-It is based on HTTP protocol so it support PUT,GET,POST,DELETE method.

In some of the tutorial,I see [HttpPost] attribute added in controller and in some cases this attribut is not added but still works fine. It may be a small question but will clear my concept a lot.Please help.

Example-below will give all detail about employee in json format by http://localhost:60486/api/products even without HttpGet or HttpPost attribute.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebApiTest.Models;

namespace WebApiTest.Controllers
{
    public class ProductsController : ApiController
    {
        Product[] products = new Product[]
           {
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
           };

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

        public Product GetProduct(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);

            return product;
        }
    }
}

Upvotes: 2

Views: 54

Answers (1)

garret
garret

Reputation: 1134

Edit2

attribute-routing-in-web-api-2

HTTP Methods Web API also selects actions based on the HTTP method of the request (GET, POST, etc). By default, Web API looks for a case-insensitive match with the start of the controller method name. For example, a controller method named PutCustomers matches an HTTP PUT request.

You can override this convention by decorating the mathod with any the following attributes:2

[HttpDelete] [HttpGet] [HttpHead] [HttpOptions] [HttpPatch] [HttpPost] [HttpPut]

By default if action method name starts with HttpAttributte example:

public IHttpActionResult GetValue(int id)
{
    return Ok();
}

api knows that this action is Get, so You don't have to add [HttpGet]

But if You want to have different action name, than You have to add [HttpAttribute]

example

[HttpGet]
public IHttpActionResult Value(int id)
{
    return Ok();
}

Edit

Base on Your example:

//This will work, because name GetAllProducts starts with Get, so api knows 
///its HttpGet, You don't have to use [HttpGet]Attribute  (but You can add 
//it, and it will work as well
public IEnumerable<Product> GetAllProducts()
{
    return products;
}

//This will work, because name GetProduct starts with Get, so api knows 
///its HttpGet, You don't have to use [HttpGet]Attribute  (but You can add 
//it, and it will work as well
public Product GetProduct(int id)
{
    var product = products.FirstOrDefault((p) => p.Id == id);

    return product;
}

but if You change names to:

//This will NOT work, because name AllProducts dont starts with Get, so api dont know what http method to use
///You have to add [HttpGet]
public IEnumerable<Product> AllProducts()
{
    return products;
}

So proper implementation (after name change) is like this:

[HttpGet]
public IEnumerable<Product> AllProducts()
{
    return products;
}

And one last thing, You change default http maping with attributes:

//This action will response to POST, not get
[HttpPost]
public IEnumerable<Product> GetAllProducts()
{
    return products;
}

Upvotes: 1

Related Questions