Skip to content

Instantly share code, notes, and snippets.

@xinmyname
Created August 25, 2014 17:30
Show Gist options
  • Save xinmyname/61af21f5761b6ab919b6 to your computer and use it in GitHub Desktop.
Save xinmyname/61af21f5761b6ab919b6 to your computer and use it in GitHub Desktop.
Render Nancy View to text
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using Nancy;
using Nancy.Hosting.Self;
using Nancy.Testing;
using Nancy.ViewEngines;
namespace NancyTesting
{
class Program
{
static void Main()
{
var hostConfig = new HostConfiguration
{
UrlReservations = new UrlReservations { CreateAutomatically = true }
};
var hostUri = new Uri("http://localhost:8089");
using (var host = new NancyHost(hostConfig, hostUri))
{
host.Start();
var bootstrapper = new DefaultNancyBootstrapper();
bootstrapper.Initialise();
INancyEngine engine = bootstrapper.GetEngine();
var request = new Request("GET", "/", "http");
NancyContext context = engine.HandleRequest(request);
Response response = context.Response;
var stream = new MemoryStream();
response.Contents(stream);
string text = Encoding.Default.GetString(stream.ToArray());
Console.WriteLine(text);
Console.WriteLine("Listening on {0}", hostUri.AbsoluteUri);
Process.Start(hostUri.AbsoluteUri);
Console.ReadLine();
}
}
}
public class SampleModule : NancyModule
{
public SampleModule()
{
Get["/"] = _ => View["Index"];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment