Skip to content

Examples Learn by example with our comprehensive code samples and demo projects

All samples shown here can be downloaded in project form from The Superscribe.Samples Github Repo. Samples showing generic functionality will be hosted using Owin but are applicable to all Superscribe project types.

Defining routes with the Fluent Api


    public class EvenNumberNode : GraphNode
    {
        public EvenNumberNode(string name)
        {
            this.activationFunction = (routeData, value) =>
            {
                int parsed;
                if (int.TryParse(value, out parsed))
                    return parsed % 2 == 0; // Only match even numbers

                return false;
            };

            this.ActionFunctions.Add("Set_" + name, (routeData, value) =>
            {
                int parsed;
                if (int.TryParse(value, out parsed))
                    routeData.Parameters.Add(name, parsed);
            });
        }
    }
            

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var define = OwinRouteEngineFactory.Create();
        
            // Set up a route that will respond only to even numbers using the fluent api
            var products = new ConstantNode("Products");
            var evenNumber = new EvenNumberNode("id");

            var helloRoute = products.Slash(evenNumber);
            helloRoute.FinalFunctions.Add(
                new FinalFunction("GET", o => "Product id: " + o.Parameters.id));

            define.Route(helloRoute);
            
            app.UseSuperscribeRouter(define)
                .UseSuperscribeHandler(define);
        }
    }
            

Unit testing Superscribe routes


    [TestClass]
    public class SuperscribeUnitTests
    {
        private IRouteEngine define;
            
        [TestInitialize]
        public void Setup()
        {
            define = RouteEngineFactory.Create();

            define.Route("Hello/World", o => "Hello World!");
            define.Route("Hello" / (String)"Name", o => "Hello " + o.Parameters.Name);
        }
        
        [TestMethod]
        public void Test_Hello_World_Get()
        {
            var routeWalker = define.Walker();
            var data = routeWalker.WalkRoute("/Hello/World", "Get", new RouteData());

            Assert.AreEqual("Hello World!", data.Response);
        }

        [TestMethod]
        public void Test_Hello_Name_Get()
        {
            var routeWalker = define.Walker();
            var data = routeWalker.WalkRoute("/Hello/Kathryn", "Get", new RouteData());

            Assert.AreEqual("Kathryn", data.Parameters.Name);
            Assert.AreEqual("Hello Kathryn", data.Response);
        }
    }
                

Using Superscribe with Asp.Net Web Api

Please note that Superscribe only supports Asp.Net Web Api 2.1 and above

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            var engine = SuperscribeConfig.Register(config);
            engine.Route("api" / Any.Controller / (Int)"id");
        }
    }
                

    public class ValuesController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new[] { "Value1", "Value2" };
        }

        public string Get(int id)
        {
            return "Value" + id;
        }

        public string Post(int id)
        {
            return "Post id: " + id;
        }
    }
                

Multiple collection resources per controller in Asp.Net Web Api

Please note that Superscribe only supports Asp.Net Web Api 2.1 and above

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            onfig.Formatters.Remove(config.Formatters.XmlFormatter);

            var define = SuperscribeConfig.Register(config);

            var blogs = define.Route("api" / "Blogs".Controller() / (Int)"blogid");

            define.Get(blogs / "Posts", To.Action("GetBlogPosts"));
            define.Get(blogs / "Tags", To.Action("GetBlogTags"));

            define.Post(blogs / "Posts", To.Action("PostBlogPost"));
            define.Post(blogs / "Tags", To.Action("PostBlogTag"));
        }
    }
                

    public class BlogsController : ApiController
    {
        public string Get()
        {
            return "Get";
        }

        public string GetById(int blogid)
        {
            return "GetById";
        }

        public string GetBlogPosts()
        {
            return "Blog Posts";
        }

        public string GetBlogTags()
        {
            return "Blog Tags";
        }

        public string GetBlogTags(string query)
        {
            return "Blog Tags - " + query;
        }

        public string PostBlogPost(int blogid)
        {
            return "Post to blog " + blogid;
        }

        public string PostBlogTag(int blogid)
        {
            return "Tag blog " + blogid;
        }
    }
                

Using Superscribe routes and modules alongside traditional/attribue routing in Asp.Net Web Api

Please note that Superscribe only supports Asp.Net Web Api 2.1 and above

    public class ValuesController : ApiController
    {
        public IEnumerable Get()
        {
            return new string[] { "value1", "value2" };
        }

        public string Get(int id)
        {
            return "value";
        }
    }
                

    public class MoviesController : ApiController
    {
        [Route("movies")]
        public IEnumerable Get()
        {
            return new[] { "The Matrix", "Lord of the Rings" };
        }
    }
                

    public class HelloWorldModule : SuperscribeModule
    {
        public HelloWorldModule()
        {
            this.Get["/"] = o => new { Message = "Hello World" };

            this.Get["Hello" / (String)"Name"] = 
                o => new { Message = "Hello, " + o.Parameters.Name };
        }
    }
                

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var define = OwinRouteEngineFactory.Create();
            var httpconfig = new HttpConfiguration();

            httpconfig.Formatters.Remove(httpconfig.Formatters.XmlFormatter);
            httpconfig.MapHttpAttributeRoutes();
            httpconfig.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { controller = "values", id = RouteParameter.Optional });

            SuperscribeConfig.RegisterModules(httpconfig, define);

            // Access values controller without falling back to web api routing
            define.Route("values".Controller());
            
            app.UseSuperscribeRouter(define)
                .UseWebApiWithSuperscribe(httpconfig, define);
        }
    }                                    
                

Using Web Api on Owin with Superscribe Router

Please note that Superscribe only supports Asp.Net Web Api 2.1 and above
For this example you'll need to install the Superscribe.WebApi.Owin package from nuget.

    public class ValuesController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public string Get(int id)
        {
            return "value";
        }
    }
                

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var define = OwinRouteEngineFactory.Create();
            var httpconfig = new HttpConfiguration();
            
            SuperscribeConfig.RegisterModules(httpconfig, define);

            define.Route("values".Controller());
            
            app.UseSuperscribeRouter(define)
                .UseWebApi(httpconfig)
                .WithSuperscribe(httpconfig, define);
        }
    }            
                

Handing control from Supersribe to web frameworks for handling requests


    public class HelloController : ApiController
    {
        public string Get()
        {
            return "Hello from Web Api";
        }
    }
                

    public class HelloModule : NancyModule
    {
        public HelloModule()
            : base("api/nancy")
        {
            this.Get["/"] = _ => "Hello from nancy";
        }
    }
                

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var httpconfig = new HttpConfiguration();
            httpconfig.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/webapi/",
                defaults: new { controller = "Hello" }
            );

            var define = OwinRouteEngineFactory.Create();

            // Set up a route that will forward requests to either web api or nancy
            define.Pipeline("api/webapi").UseWebApi(httpconfig);
            define.Pipeline("api/nancy").UseNancy();
            
            app.UseSuperscribeRouter(define);
        }
    }
                

Owin Hello World


    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var define = OwinRouteEngineFactory.Create();
            
            define.Route("Hello/World", o => "Hello World");
            
            app.UseSuperscribeRouter(define)
                .UseSuperscribeHandler(define);
        }
    }
                

Using modules in Owin to accept and return Json


    public class Product
    {
        public int Id { get; set; }

        public string Description { get; set; }

        public double Price { get; set; }

        public string Category { get; set; }
    }
                

    public class ProductsModule : SuperscribeOwinModule
    {
        private readonly List<Product> products = new List<Product>
        {
            new Product { Id = 1, Description = "Shoes", Price = 123.50, Category = "Fashion"},
            new Product { Id = 2, Description = "Hats", Price = 55.20, Category = "Fashion"},
            new Product { Id = 3, Description = "iPad", Price = 324.50, Category = "Electronics"},
            new Product { Id = 4, Description = "Kindle", Price = 186.20, Category = "Electronics"},
            new Product { Id = 5, Description = "Dune", Price = 13.50, Category = "Books"}
        };

        public ProductsModule()
        {
            this.Get["Products"] = o => products;

            this.Get["Products" / (Int)"Id"] = o => 
                products.FirstOrDefault(p => o.Parameters.Id == p.Id);

            this.Get["Products" / (String)"Category"] = o => 
                products.Where(p => o.Parameters.Category == p.Category);

            this.Post["Products"] = o =>
                {
                    var product = o.Bind<Product>();
                    return new { Message = string.Format(
                        "Received product id: {0}, description: {1}", 
                        product.Id, product.Description) };
                };
        }
    }
                

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var options = new SuperscribeOwinOptions();
            options.MediaTypeHandlers.Remove("text/html");
            options.MediaTypeHandlers.Add("application/json", new MediaTypeHandler
            {
                   Write = (env, o) => env.WriteResponse(JsonConvert.SerializeObject(o)),
                   Read = (env, type) =>
                   {
                       object obj;
                       using (var reader = new StreamReader(env.GetRequestBody()))
                       {
                           obj = JsonConvert.DeserializeObject(reader.ReadToEnd(), type);
                       };

                       return obj;
                   }
               });

            // Replace text/html with json handler so example works in a browser
            options.MediaTypeHandlers.Add("text/html", options.MediaTypeHandlers["application/json"]);

            var engine = OwinRouteEngineFactory.Create(options);

            app.UseSuperscribeRouter(engine)
                .UseSuperscribeHandler(engine);
        }
    }
                

Using parameters captured during routing in other Middleware


    public class SayHello
    {
        private readonly Func<IDictionary<string, object>, Task> next;

        public SayHello(Func<IDictionary<string, object>, Task> next)
        {
            this.next = next;
        }

        public async Task Invoke(IDictionary<string, object> environment)
        {
            var parameters = environment["route.Parameters"] as IDictionary<string, object>;
            if (parameters != null && parameters.ContainsKey("Name"))
            {
                await environment.WriteResponse("Hello " + parameters["Name"]);    
            }
        }
    }
                

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var define = OwinRouteEngineFactory.Create();

            define.Route("Hello" / (String)"Name", o => "Hello");

            app.UseSuperscribeRouter(define)
                .Use<SayHello>();
        }
    }
                

Building and executing an Owin pipeline branch during routing

                
    public class PadResponse
    {
        private readonly Func<IDictionary<string, object>, Task> next;

        private readonly string tag;

        public PadResponse(Func<IDictionary<string, object>, Task> next, string tag)
        {
            this.next = next;
            this.tag = tag;
        }

        public async Task Invoke(IDictionary<string, object> environment)
        {
            await environment.WriteResponse("<" + tag + ">");
            await this.next(environment);
            await environment.WriteResponse("</" + tag + ">");
        }
    }
                

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var define = OwinRouteEngineFactory.Create();

            define.Pipeline("Hello").Use(typeof(PadResponse), "h1");
            define.Pipeline("Goodbye").Use(typeof(PadResponse), "marquee");
            
            define.Route("Hello/World", o => "Hello World");
            define.Route("Goodbye/World", o => "Goodbye World");

            app.UseSuperscribeRouter(define)
              .UseSuperscribeHandler(define);            
        }
    }
                

Use Superscribe with DotNetDoodle.Owin.Dependencies to configure your dependency container based on your routes

Please note that Superscribe only supports Asp.Net Web Api 2.1 and above
For this example you'll need to install DotNetDoodle.Owin.Dependencies and package from nuget. For more information on DotNetDoodle.Owin.Dependencies, please see this post by Tuberk Ugurlu.
   
    public class ValuesController : ApiController
    {
        private readonly IRepository repository;

        public ValuesController(IRepository repository)
        {
            this.repository = repository;
        }

        public IEnumerable<string> Get()
        {
            return this.repository.Values();
        }
    }           
                
   
    public interface IRepository
    {
        IEnumerable<string> Values();
    }

    public class Repository : IRepository
    {
        public IEnumerable<string> Values()
        {
            return new[] { "value1", "value2" };
        }
    }

    public class ApiRepository : IRepository
    {
        public IEnumerable<string> Values()
        {
            return new[] { "value3", "value4" };
        }
    }
                
  
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            var engine = OwinRouteEngineFactory.Create();

            var httpconfig = new HttpConfiguration();
            httpconfig.Formatters.Remove(httpconfig.Formatters.XmlFormatter);
            SuperscribeConfig.Register(httpconfig, engine);

            engine.Route("Values".Controller());
            engine.Route("Api" / "Values".Controller());

            engine.Pipeline("Api").Use<ApiDependencies>();

            appBuilder.UseAutofacContainer(this.RegisterServices())
                .UseSuperscribeRouter(engine)
                .UseWebApiWithContainer(httpconfig)
                .WithSuperscribe(httpconfig, engine);
        }

        public IContainer RegisterServices()
        {
            var builder = new ContainerBuilder();

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterOwinApplicationContainer();

            builder.RegisterType<Repository>()
                   .As<IRepository>()
                   .InstancePerLifetimeScope();
            
            return builder.Build();
        }
    }
                

    public class ApiDependencies
    {
        private readonly Func<IDictionary<string, object>, Task> next;

        public ApiDependencies(Func<IDictionary<string, object>, Task> next)
        {
            this.next = next;
        }

        public async Task Invoke(IDictionary<string, object> environment)
        {
            var container = environment.GetRequestContainer() as ILifetimeScope;
            var newBuilder = new ContainerBuilder();
            newBuilder.RegisterType<ApiRepository>()
                   .As<IRepository>()
                   .InstancePerLifetimeScope();

            newBuilder.Update(container.ComponentRegistry);

            await this.next(environment);
        }
    }