Skip to content

Instantly share code, notes, and snippets.

@Vidarls
Created October 5, 2011 13:33
Show Gist options
  • Save Vidarls/1264433 to your computer and use it in GitHub Desktop.
Save Vidarls/1264433 to your computer and use it in GitHub Desktop.
Super naive nancy stuff
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Events</title>
</head>
<body>
<h1>Events</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Data</th>
</tr>
</thead>
<tbody>
@Each
<tr>
<td>@Current.Id</td>
<td>@Current.Data</td>
</tr>
@EndEach
</tbody>
</body>
</html>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting...");
var host = new Nancy.Hosting.Self.NancyHost(new Uri("http://localhost:80/"),
new DefaultNancyBootstrapper());
host.Start();
Console.WriteLine("Started.");
Console.WriteLine("Enter to exit.");
Console.ReadLine();
host.Stop();
}
}
public class Events : NancyModule
{
public Events() : base("events")
{
Get["/"] = (_) =>
{
var db = Database.Open("SomeConnectionString");
var events = db.Events.All().OrderByIdDescending();
return View["EventList", events];
};
}
}
@serialseb
Copy link

public class Config : IConfigurationSource { 
  public void Configure() {
    using (OpenRastaConfiguration.Manual) {
      ReosurceSpace.Has.ResourcesOfType<Events>()
                       .AtUri("/")
                       .HandledBy<EventsHandler>()
                       .RenderedByAspx("~/myView.aspx");
    }
  }
}

public class Events {
  public dynamic Items { get;set; }
}

public class EventsHandler {
  public Events Get() {
    var db = Database.Open("SomeConnectionString");
    return new Events { Items = db.Events.All().OrderByIdDescending(); }
  }
}

<%@ Page Language="C#" Inherits="ResourceView<Events>" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Events</title>
</head>
<body>
<h1>Events</h1>
<table>
<thead>
  <tr>
    <th>Id</th>
    <th>Data</th>       
  </tr>
</thead>
<tbody>
<% foreach(var item in Resource.Items) { %>
  <tr>
    <td><%= item.Id %></td>
    <td><%= item.Data %></td>       
  </tr>
<% } %>
</tbody>
</body>
</html>


class Host : HttpListenerHost {
  protected override void ConfigureRootDependencies(IDependencyResolver resolver) {
    resolver.AddDependencyInstance<IConfigurationSource>(new Config());
    return base.ConfigureRootDependencies(resolver);
  }
}
class Program {
  static void Main(string[] args) {
      Console.WriteLine("Starting...");
      using(var host = new Host()) {
        host.Initialize(new[]{"http://localhost:80"}, "/", null);
        host.StartListening();
        Console.WriteLine("Started.");
        Console.WriteLine("Enter to exit.");
        Console.ReadLine();
      }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment