Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to add location header to responses in NancyFx

Tags:

rest

c#

nancy

I'm using Nancy as a rest back-end for my application. Currently I've managed to add location header to my responses with this hack :

var headerUri = Request.Url.SiteBase + Request.Path + "/" + processedModel.Id.ToString();

Response response = new JsonResponse(processedModel,Response.Serializers.First(s => s.CanSerialize("application/json")));

response.Headers["Location"] = headerUri;

return response;

As I also want to return only json results, what would be the best solution?

like image 376
user3550283 Avatar asked Jul 20 '15 14:07

user3550283


1 Answers

If you only want to return JSON you can do:

return Response.AsJson(model);

If you want to redirect the user you can do:

return Response.AsRedirect("url");

If you want to append headers to your JSON result you can use .WithHeader(...) like so:

return Response.AsJson(model).WithHeader("bananas", "are always yellow");

Here's a quick example where I return a image with headers:

return Response.FromStream(thumbStream, "image/png")
               .WithHeader("FileId", file.Id)
               .WithHeader("FileName", file.Name);
like image 171
Phill Avatar answered Sep 23 '22 12:09

Phill