Skip to content

A simple web framework for .NET inspired by Express JS

License

Notifications You must be signed in to change notification settings

lucasgodshalk/Antsy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Antsy Web Framework

Build status NuGet

This is a web framework for people who just want to get going with as few lines as possible. Ideal for quickly making small applications.

Here is a simple example:

using Antsy;

class Program
{
    static void Main(string[] args)
    {
        var host = new AntsyHost(port: 8000);
        host.Get("/hello", (req, res) =>
        {
            res.Text("hello world");
        });
        host.Run();
        //Hit localhost:8000/hello
    }
}

Antsy lets ASP.NET Core do most of the heavy lifting so that it doesn't reinvent the wheel, but the api surface is inspired by Express JS.

The name is uncomfortably close to Nancy. If this actually becomes popular, my sincerest appologies to the Nancy maintainers. Any confusion was both entirely avoidable, and my fault.

Note that the host will not listen for https connections (just stick your favorite reverse proxy in front and do https termination there). All json model binding uses Json.net.

This project targets netstandard 2.0 so if you're running dotnet core or a modern full framework this library should work for you.

API

Constructor
new AntsyHost(int port = 80);
Methods (AntsyHost)

The path variable should follow asp.net routing rules.

//Switch between sync and async. Heaven help you if your sync call blocks.
host.Get(string path, Func<AntsyRequest, AntsyResponse, Task>);
host.Get(string path, Action<AntsyRequest, AntsyResponse>);
host.Post(string path, Func<AntsyRequest, AntsyResponse, Task>);
host.Post(string path, Action<AntsyRequest, AntsyResponse>);
host.Delete(string path, Func<AntsyRequest, AntsyResponse, Task>);
host.Delete(string path, Action<AntsyRequest, AntsyResponse>);
//folderRoot is relative to the current directory.
host.StaticFiles(string path, string folderRoot);
Request & Response

The req and res are the standard ASP.NET HttpRequest and HttpResponse objects, but gain helper methods to make them line up more with the Express API.

Helper methods on the request object:

//Deserializes the body json to an object (using Json.net formatting).
req.BodyJson<T>();
//Returns the body as text.
req.BodyText();

Helper methods on the response object:

//Accepts either a POCO or a string. Formats response as json.
res.Json(object obj);
//Formats the response as text.
res.Text(string text);
//Formats the response as html (string or file).
res.Html(string html);
//Serves the file as a download.
res.Download(string filepath);
res.Download(string filename, Stream filestream);
//If I can wrangle it, I'd like to see something like res.Razor(string pageName, object model) in the future.

These helper methods just make sure the response headers are properly formatted (Content-Type, and friends).

License

MIT

About

A simple web framework for .NET inspired by Express JS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published