Skip to content

Instantly share code, notes, and snippets.

@satish860
Created October 15, 2012 12:57
Show Gist options
  • Save satish860/3892324 to your computer and use it in GitHub Desktop.
Save satish860/3892324 to your computer and use it in GitHub Desktop.
/// <summary>
/// Url for the POST is http://localhost/api/todo
/// </summary>
/// <param name="item">Creates the Todo ITEM</param>
/// <returns></returns>
public HttpResponseMessage Post(TodoItem item)
{
try
{
if (string.IsNullOrEmpty(item.Description))
{
ValidationResult result = new ValidationResult { Name = "Description", ErrorMessage = "Description should not be empty" };
return Request.CreateResponse<ValidationResult>(HttpStatusCode.BadRequest, result);
}
var response = Request.CreateResponse<TodoItem>(HttpStatusCode.Created, new TodoItem { });
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "Todo", id = item.Id }));
return response;
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
/// <summary>
/// Url for the POST is http://localhost/api/todo
/// </summary>
/// <param name="item">Creates the Todo ITEM</param>
/// <returns></returns>
public HttpResponseMessage Post(TodoItem item)
{
var response=Request.CreateResponse<TodoItem>(HttpStatusCode.Created, new TodoItem { });
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "Todo", id = item.Id }));
return response;
}
namespace WebAPIKoans.Controllers
{
public class TodoController : ApiController
{
/// <summary>
/// Url-- POST http://localhost/api/todo
/// </summary>
/// <param name="item">Creates the Todo ITEM</param>
/// <returns></returns>
public HttpResponseMessage Post(TodoItem item)
{
}
}
}
/// <summary>
/// Url for the POST is http://localhost/api/todo
/// </summary>
/// <param name="item">Creates the Todo ITEM</param>
/// <returns></returns>
public HttpResponseMessage Post(TodoItem item)
{
if (string.IsNullOrEmpty(item.Description))
{
ValidationResult result = new ValidationResult { Name = "Description", ErrorMessage = "Description should not be empty" };
return Request.CreateResponse<ValidationResult>(HttpStatusCode.BadRequest, result);
}
var response=Request.CreateResponse<TodoItem>(HttpStatusCode.Created, new TodoItem { });
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "Todo", id = item.Id }));
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment