Presented by David Roberts / @davidroberts63
get "/sayhello/{name}" do "Hello " + {name} end
public class HomeModule : NancyModule { public HomeModule() { Get[″/sayhello/{name}″] = input => return ″Hello ″ + input.Name; } }
Using ScriptCS
scriptcs -install ScriptCs.Nancy; 'Require<NancyPack>().Get("/",_=>"Hi").Host();'|Out-File s.x; scriptcs s.x #dropsmike @NancyFx @scriptcsnetMost of the time writing a Nancy app would look like what you just saw. A Visual Studio project with that NancyModule in place. However with the creation of ScriptCS it can get smaller in some uses. Briefly, ScriptCS is a scripting environment for C# and it also has a REPL environment. What you see is an entire web application in Nancy. Installing the dependencies, creating the application file, and running it. All fitting within a tweet with enough room for shout outs to Nancy and ScriptCS.
Uses its own Request/Response objects.Not dependent on ASP.NET
Ewwww.
Note, WCF NOT inspired by Ruby. Because Ruby wouldn't do that.
Done automatically or controll it yourself
Get["/"] = p => return "Hello Jupiter!";
Get["/"] = parameters => { return Negotiate .WithModel(new RatPack {FirstName = "Nancy "}) .WithMediaRangeModel("text/html", new RatPack {FirstName = "Nancy for html"}) .WithView("negotiatedview") .WithHeader("X-Custom", "SomeValue"); };Like in Sinatra, Nancy does content negotiation out of the box. Just pass back the data and Nancy will process it into an html page, json, or xml response depending on the requests Accept header. You can also add custom ones easily. Control the negotiation yourself if you want with the Negotiate property.
Custom Model Binding
public class CustomModelBinder : Nancy.ModelBinding.IModelBinder { . . . }Because of the SDHP customizing is very easy. Just define the class and Nancy will pick up most customizations automatically. Custom model binding for instance.
Custom View Engine
public class CustomViewEngine : Nancy.ViewEngines.IViewEngine { . . . }
Test the whole Nancy pipeline quickly with the Browser object.
public void Should_return_status_ok_when_route_exists() { var bootstrapper = new DefaultNancyBootstrapper(); var browser = new Browser(bootstrapper, defaults: to => to.Accept(″application/json″)); var result = browser.Get(″/″, with => { with.HttpRequest(); } ); Assert.Equal(HttpStatusCode.OK, result.StatusCode); }Its testability is easily one of the best features of Nancy. Because it's not dependent on ASP.NET, exercising the entire nancy pipeline including any custom model binders, request/response pipeline customizations etc. is a breeze. The Browser object allows you to easily test your modules individually or verify the Nancy setup entirely. The Browser object also allows you to verify your response output be it JSON, XML, or HTML without having to go through an actual browser.
twitter, github, bitbucket, jabbr, IRC
NancyFx.org That's all I have to talk about Nancy right now. There is a lot more too it and plenty of documentation to go with it. You can see that Ruby is having a very good influence on .NET with Nancy, OWIN (the Rack for .NET) and C# scripting with ScriptCS. So take a look to see that .NET is headed in a great direction.