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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With