locked
WebAPI: How to do attribute routing for Post, Put and Delete verbs RRS feed

  • Question

  • User264732274 posted

    i am new in web api. some how i manage to do attribute routing for get type request but do not know how to do it for

    Post, Put and Delete verbs

    please see my sample code and come with modified code where some one would show me how to do attribute routing for Post, Put and Delete verbs.

    [RoutePrefix("api/customer")]
    public class CustomerController : ApiController
    {
        static readonly ICustomerRepository repository = new CustomerRepository();
    
        [HttpGet, Route("GetAll")]
        public IEnumerable<Customer> GetAllCustomers()
        {
            return repository.GetAll();
        }
    
        [HttpGet, Route("GetByID/{customerID}")]
        public Customer GetCustomer(string customerID)
        {
            Customer customer = repository.Get(customerID);
            if (customer == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return customer;
        }
    
        [HttpGet, Route("GetByCountryName/{country}")]
        public IEnumerable<Customer> GetCustomersByCountry(string country)
        {
            return repository.GetAll().Where(
                c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
        }
    
        public HttpResponseMessage PostCustomer(Customer customer)
        {
            customer = repository.Add(customer);
            var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);
    
            string uri = Url.Link("DefaultApi", new { customerID = customer.CustomerID });
            response.Headers.Location = new Uri(uri);
            return response;
        }
    
        public void PutProduct(string customerID, Customer customer)
        {
            customer.CustomerID = customerID;
            if (!repository.Update(customer))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
    
        public void DeleteProduct(string customerID)
        {
            Customer customer = repository.Get(customerID);
            if (customer == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            repository.Remove(customerID);
        }
    }
    

    just show me how to set attribute routing for these following Post, Put and Delete verbs type actions

    PostCustomer, PutProduct, DeleteProduct

    Friday, August 5, 2016 9:59 AM

Answers

  • User36583972 posted

    Hi sudip_inn,

    You can refer the following code and make a sample on your side.

       static readonly ICustomerRepository repository = new CustomerRepository();
    
            [HttpGet, Route("GetAll")]
            public IEnumerable<Customer> GetAllCustomers()
            {
                return repository.GetAll();
            }
    
            [HttpGet, Route("GetByID/{customerID}")]
            public Customer GetCustomer(string customerID)
            {
                Customer customer = repository.Get(customerID);
                if (customer == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return customer;
            }
    
            [HttpGet, Route("GetByCountryName/{country}")]
            public IEnumerable<Customer> GetCustomersByCountry(string country)
            {
                return repository.GetAll().Where(
                    c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
            }
    
            [HttpPost, Route("AddCustomer")]
            public HttpResponseMessage PostCustomer([FromBody]Customer customer)
            {
                customer = repository.Add(customer);
                var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);
    
                string uri = Url.Link("DefaultApi", new { customerID = customer.CustomerID });
                response.Headers.Location = new Uri(uri);
                return response;
            }
    
            [HttpPut, Route("UpdateCustomer/{customerID}")]
            public void PutProduct([FromUri]string customerID, [FromBody]Customer customer)
            {
                customer.CustomerID = customerID;
                if (!repository.Update(customer))
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }
    
            [HttpDelete, Route("DeleteCustomer/{customerID}")]
            public void DeleteProduct([FromUri]string customerID)
            {
                Customer customer = repository.Get(customerID);
                if (customer == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                repository.Remove(customerID);
            }
    
    

    Attribute Routing in ASP.NET Web API 2:

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 5:45 AM