An open API service indexing awesome lists of open source software.

https://github.com/nklbdev/routy-csharp


https://github.com/nklbdev/routy-csharp

Last synced: about 1 year ago
JSON representation

Awesome Lists containing this project

README

          

# Routy is a routing library for building RESTful APIs in C#

(it is still in the experimental stage)

## Pros:

* It does not use reflection to create controllers and set action parameters
* It does not use string-based URL templates
* Explicit specification of all type conversions
* It is possible to use not only in RESTful APIs, but everywhere, where you need to choose strategies for processing URLs
* It supports asynchronous handlers
* It allows detection of errors in method signatures using compiler messages

You can build your routing in this way:

```cs
public static void Main(string[] args)
{
var cts = new CancellationTokenSource();
var ioc = new IoC();

new Server(Resource>
// Pass default controller factory to root resource
.Root(ioc.Resolve,
methods => methods
// You can declare HTTP methods with many handlers
// (Declare simple handlers first)
.Method("get", h => h
// You can bind controller method
.Query(q => q, cf => cf().Index)
// or other controller method
.Query(q => q, cf => ioc.Resolve().Index)
// or async controller method
.Query(q => q, cf => cf().IndexAsync)
// or pass cancellation token to async method
.Query(q => q.CancellationToken(), cf => cf().IndexAsync)
// You also can bind another static method
.Query(q => q, cf => Index)
// or static asyncs
.Query(q => q, cf => IndexAsync)
.Query(q => q.CancellationToken(), cf => IndexAsync)
// You can declare required query parameters
.Query(q => q.Single("a", int.Parse), cf => Index)
// Or not required with default value
.Query(q => q.Single("a", int.Parse, 0), cf => Index)
// Or array-parameters
.Query(q => q.Array("a", int.Parse), cf => Index)
// You can extract any data from context by your own extractor
// and declare it as parameter for your method
.Query(q => q.Context(ct => ParseEntity(ct.InputStream), 4), cf => Index)
// And of cource you can rearrange your
// multiple parameters as you wish
.Query(q => q
.Single("a", bool.Parse)
.Single("b", int.Parse)
.Single("c", int.Parse),
cf => (a, b, c) => Index(b, a))),
// Declare nested resources
root => root
// With concrete name
// And with default controller factory from parent resource
.Named("about", methods => methods
.Method("get", h => h
.Query(q => q, cf => cf().About)))
// Or with other controller factory
.Named("news", ioc.Resolve,
methods => methods
.Method("get", h => h
.Query(q => q
.Single("page", int.Parse, 0)
.Single("order", bool.Parse, false),
cf => cf().Index)),
news => news
// Or use a value as a name
// (with controller factory from parent resource or new)
.Valued(int.Parse,
methods => methods
.Method("get", h => h
.Query(q => q, cf => cf().Get))))))
.Run(cts.Token).Wait();
}
```