lundi 31 octobre 2016

Negiotiated media-range redirect with NancyFX

I've got a NancyFX module which serves both the view as well as json-based API results where as the response content is negotiated using media range filters. What I'm trying to achive is the following:

When deleting entity XXX...

  • for HTML -> redirect to module path
  • for JSON -> return status code OK

My current code:

return Negotiate
    .WithStatusCode(HttpStatusCode.OK)
    .ForHtml(new RedirectResponse(ModulePath))
    .ForJson(Nancy.Response.NoBody);

Where as ForHTML and ForJson are extension methods which look like this:

    public static Negotiator ForJson(this Negotiator negotiator, Func<object> resultFactory)
        => negotiator.WithMediaRangeModel(MediaRanges.Json, resultFactory);

    public static Negotiator ForHtml(this Negotiator negotiator, Func<object> resultFactory)
        => negotiator.WithMediaRangeModel(MediaRanges.Html, resultFactory);

The media ranges are text/html and application/json respectively.

However it doesn't really work. I always get just the status code back, no matter if I'm accessing the module from a browser or a REST client.




Aucun commentaire:

Enregistrer un commentaire