Created
October 17, 2012 07:56
-
-
Save skayred/3904289 to your computer and use it in GitHub Desktop.
Nancy and ASP.NET Web API comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace aspapi.Controllers | |
{ | |
public class TaskApiController : ApiController | |
{ | |
private readonly ITaskRepository taskRepository; | |
public TaskApiController() | |
{ | |
taskRepository = new TaskRepository(); | |
} | |
public IEnumerable<Task> Get() | |
{ | |
return taskRepository.All; | |
} | |
public Task Get(int id) | |
{ | |
var task = taskRepository.Find(id); | |
if (task == null) | |
{ | |
throw new HttpResponseException(new HttpResponseMessage | |
{ | |
StatusCode = HttpStatusCode.NotFound, | |
Content = new StringContent("Task not found") | |
}); | |
} | |
return task; | |
} | |
public HttpResponseMessage Post(Task task) | |
{ | |
taskRepository.InsertOrUpdate(task); | |
taskRepository.Save(); | |
var response = Request.CreateResponse<Task>(HttpStatusCode.Created, task); | |
string uri = Url.Route(null, new { id = task.Id }); | |
response.Headers.Location = new Uri(Request.RequestUri, uri); | |
return response; | |
} | |
public Task Put(Task task) | |
{ | |
try | |
{ | |
taskRepository.InsertOrUpdate(task); | |
taskRepository.Save(); | |
} | |
catch (Exception) | |
{ | |
throw new HttpResponseException(new HttpResponseMessage | |
{ | |
StatusCode = HttpStatusCode.NotFound, | |
Content = new StringContent("Task not found") | |
}); | |
} | |
return task; | |
} | |
public HttpResponseMessage Delete(int id) | |
{ | |
taskRepository.Delete(id); | |
taskRepository.Save(); | |
return new HttpResponseMessage | |
{ | |
StatusCode = HttpStatusCode.NoContent | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace nancyapi | |
{ | |
public class ApiModule : Nancy.NancyModule | |
{ | |
private ITaskRepository taskRepository; | |
public ApiModule() { | |
taskRepository = new TaskRepository(); | |
Get["/api"] = x => taskRepository.All; | |
Get["/api/{id}"] = x => taskRepository.Find(x.id); | |
Post["/api"] = pars => | |
{ | |
var task = new Task(); | |
task.Description = Request.Form.description; | |
task.Priority = Request.Form.priority; | |
task.CreatedOn = Request.Form.createdon; | |
var id = taskRepository.InsertOrUpdate(task); | |
taskRepository.Save(); | |
return JsonEncode(id); | |
}; | |
Put["/api/{id}"] = pars => | |
{ | |
var task = new Task(); | |
task.Id = pars.id; | |
task.Description = Request.Form.description; | |
task.Priority = Request.Form.priority; | |
task.CreatedOn = Request.Form.createdon; | |
var id = taskRepository.InsertOrUpdate(task); | |
taskRepository.Save(); | |
return JsonEncode(id); | |
}; | |
Delete["/api/{id}"] = x => taskRepository.Delete(x.id); | |
} | |
} | |
} |
Agree with @jrharmon. Even though this is 3 years old it is appearing in google searches. It is not a like for like comparison... should be updated to be more realistic.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems like you are doing way more error checking on the Web API version than the Nancy version, which un-fairly complicates the Web API code. If there is any automatic error handling that Nancy does, Web API could do the same to automatically turn un-handled exceptions into the proper responses.