Kayak

[Path()] attribute

The [Path()] attribute is used to map requests to method invocations. Simply slap this baby on an instance method of a class with a public no-args constructor, and the Kayak framework will pick it up and invoke that method when it receives a request for that path.

The [Path()] attribute accepts a single string argument, the path that you are trying to map:

public class MyAPI { [Path("/api/foo")] public void Stuff() { Console.WriteLine("got a request for /api/foo!"); } }

The path string allows some special syntax that allows a component of the path to vary and map to a method parameter:

public class MyAPI { [Path("/api/foo/{bar}")] public void Stuff(string bar) { Console.WriteLine("got a request for /api/foo/" + bar + "!"); } }

What's really going on here is that when MethodMap finds a method to invoke for the request, the a NameValueDictionary is constructed for the variable path parameters inside the curly braces, and added to KayakContext.Items with the key MethodMap.PathParamsContextKey. So, parameters can be accessed that way as well:

// extend from KayakService for access to the current KayakContext public class MyAPI : KayakService { [Path("/api/foo/{bar}")] public void Stuff() { NameValueDictionary params = Context.Items[MethodMap.PathParamsContextKey]; Console.WriteLine("/api/foo/" + params["bar"] + "!"); } }

Methods marked only with [Path()] will only be invoked for if the HTTP request verb is GET. To respond to requests that use other verbs, you can use one or more [Verb()] attributes.