Skip to content

Instantly share code, notes, and snippets.

@skayred
Created October 17, 2012 07:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save skayred/3904289 to your computer and use it in GitHub Desktop.
Save skayred/3904289 to your computer and use it in GitHub Desktop.
Nancy and ASP.NET Web API comparison
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
};
}
}
}
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);
}
}
}
@waynebrantley
Copy link

In Nancy version, cant you just do this:

var task = this.Bind();
var id = taskRepository.InsertOrUpdate(task);
taskRepository.Save();

            return JsonEncode(id);

@romko391
Copy link

ok, but is there a performance difference?

@jrharmon
Copy link

jrharmon commented Oct 7, 2015

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.

@conorjgallagher
Copy link

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