Skip to content

Instantly share code, notes, and snippets.

@johnnonolan
Last active August 29, 2015 14:18
Show Gist options
  • Save johnnonolan/2c3725a73f3da13a7f94 to your computer and use it in GitHub Desktop.
Save johnnonolan/2c3725a73f3da13a7f94 to your computer and use it in GitHub Desktop.
negotiation
using System;
using Nancy;
using Nancy.Hosting.Self;
using Nancy.Responses.Negotiation;
using System.Collections.Generic;
using Nancy.Responses;
namespace negotiation
{
class MainClass
{
public static void Main (string[] args)
{
using (var host = new NancyHost(new Uri("http://localhost:1234")))
{
StaticConfiguration.DisableErrorTraces = false;
host.Start();
Console.WriteLine ("Hello from nancy");
Console.ReadLine();
}
}
}
public class MyRequestModel {}
public class MyModule : NancyModule
{
public MyModule ()
{
Get ["/"] = c => {
return Negotiate.WithModel(new MyRequestModel());
};
}
}
public class MyResponseProcessor : IResponseProcessor
{
private MediaRange MediaType = new MediaRange("application/vnd.blah.gnn.v1+json");
public IEnumerable<Tuple<string, MediaRange>> ExtensionMappings { get; }
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
{
var contentTypeResult = (MediaType).MatchesWithParameters (requestedMediaRange) ? MatchResult.ExactMatch : MatchResult.DontCare;
return new ProcessorMatch {
ModelResult = MatchResult.ExactMatch,
RequestedContentTypeResult = contentTypeResult
};
}
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context)
{
var myModel = new {Success = true};
return new JsonResponse (myModel, new DefaultJsonSerializer());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment