Web API Put Method Example Asp.net C#

In this article we will learn how to write Web API Put Method in Asp.net Framework using C#.

In example we are trying to put a client object to be updated in database, just to keep this tutorial web API call specific, I have removed the database calling part ( you can use Entity Framework , Ado.net or anything to submit the data to database)

public void Post(int id, [FromBody]ClientModel client)            
{
    tbClient c = new tbClient();
    c.ClientId = id;
    c.CompanyName = client.CompanyName;
    c.ContactPerson = client.ContactPerson;
    c.Email = client.Email;
    c.Phone = client.Phone;    
        
    //add this details to database            
}

Web API Put Method

Put with HttpResponseMessage Return type

Put is update method, is almost like Post Method, the only difference is that this method has Id parameter, for which the data to be updated in database

public HttpResponseMessage Put(int id, [FromBody]ClientModel client)
{
    try
    {
        Client c = new Client();
        c.ClientId = id;
        c.CompanyName = client.CompanyName;
        c.ContactPerson = client.ContactPerson;
        c.Email = client.Email;
        c.Phone = client.Phone;


        // update client in database for this clientId
        c.ClientId = id;


        var returnMessage = Request.CreateResponse(HttpStatusCode.Created, c);
        returnMessage.Headers.Location = new Uri(Request.RequestUri + "/" + c.ClientId.ToString());
        return returnMessage;
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
    }
}
CreateResponse and CreateErrorResponse

Notice the below two lines in above code, creating Response object, both methods return HttpResponseMessage object.

  var returnMessage = Request.CreateResponse(HttpStatusCode.Created, c);
var returnErrMessage = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
Test the Put Method with Web Client

Once you are ready with your Put method in Web API, you probably need to test if things are working fine, and without creating any complex UI client you can Test Web API

Asp.net Web API Async Put Method

This Web API Put Method example in Asp.net Core.

This is an asynchronous method, you can see how to implement error handling in Web API Put method.

[HttpPost]
public async Task<HttpResponseMessage> Put(int id, [FromBody]StudentModel model)
{
    Student _student = new Student();
    _student.StuId = id;
    _student.FirstName = model.FirstName;
    _student.Email = model.Email;
    _student.Cotact = model.Cotact;
    _student.City = model.City;
    HttpResponseMessage returnMessage = new HttpResponseMessage();
    try
    {
        // add details to database;
        string message = ($"Student Updated - {_student.StuId}");
        returnMessage = new HttpResponseMessage(HttpStatusCode.Created);
        returnMessage.RequestMessage = new HttpRequestMessage(HttpMethod.Post, message);
    }
    catch (Exception ex)
    {
        returnMessage = new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
        returnMessage.RequestMessage = new HttpRequestMessage(HttpMethod.Post, ex.ToString());
    }
    return await Task.FromResult(returnMessage);
}

Now if you see the difference between put and post, is just an additional id parameter, which indicates that this method is for updating existing data.

In new Asp.net framework, you can write any number of update method , think of real-time business requirement, suppose you are developing a student management API, where you want to develop functionalities like UpdateContact, UpdateCertification, UpdateBlog etc.

[HttpPost]
public async Task<HttpResponseMessage> UpdateContact([FromBody]StudentModel model)
{
	HttpResponseMessage returnMessage = new HttpResponseMessage();
	return await Task.FromResult(returnMessage);
}

[HttpPost]
public async Task<HttpResponseMessage> UpdateCertification([FromBody]StudentModel model)
{
	HttpResponseMessage returnMessage = new HttpResponseMessage();
	return await Task.FromResult(returnMessage);
}

The above methods are very easy to understand, you can develop multiple functions as per your business requirement, the methods are asynchronous and error handling implementation done such way that can communicate back with right message, so API consumer will know what exactly gone wrong.

 
Asp.Net Web API Put Example
Learn Web API: creating API using C# .Net, post, update, delete, read using .net API, set security, check authentication, testing.

C# web service interview questions

Learn API development
Asp.Net C# Web API Examples | Join Asp.Net MVC Course