Skip to content

Instantly share code, notes, and snippets.

@benfoster
benfoster / gist:3515152
Created August 29, 2012 16:21
Automatically bind specific types from the Uri (without the FromUri attribute)
// bind all commands decorated with IFromUriCommand from the Uri.
config.ParameterBindingRules.Insert(0,
desc => typeof(IFromUriCommand).IsAssignableFrom(desc.ParameterType)
? new FromUriAttribute().GetBinding(desc)
: null);
@benfoster
benfoster / gist:3515675
Created August 29, 2012 17:04
Adding resource links in ASP.NET Web Api payloads
public class ResourceLinksActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
ResourceModel payload;
if (actionExecutedContext.Response.TryGetContentValue<ResourceModel>(out payload))
{
payload.BuildResourceLinks(actionExecutedContext.Request);
}
@benfoster
benfoster / gist:3517720
Created August 29, 2012 19:37
Web API Resource Links
{
page:2,
pageSize:1,
totalCount:3,
totalPages:3,
hasPreviousPage:true,
hasNextPage:true,
results:[
{
id:193,
@benfoster
benfoster / gist:3524083
Created August 30, 2012 08:17
Example of relation links
{
"page":1,
"pageSize":1,
"totalCount":4,
"totalPages":4,
"hasPreviousPage":false,
"hasNextPage":true,
"results":[
{
"id":129,
public interface IFoo
{
}
public interface IFooCollection
{
IEnumerable<IFoo> Items { get; }
}
@benfoster
benfoster / gist:3551750
Created August 31, 2012 11:38
How my api is shaping up
/sites -- get all sites
/sites/65 -- get site with id 65
/sites/65/posts -- get posts from site with id 65
/sites/65/posts/10 -- get post with id 10 from site with id 65
/sites/65/posts/tags -- get a list of tag counts for all posts from site with id 65
/sites/65/posts/tags/web-api -- get all posts from site with id 65 tagged with web-api
query parameters:
@benfoster
benfoster / gist:3555524
Created August 31, 2012 16:35
AtomPubMediaTypeFormatter for ASP.NET Web API
public class AtomPubMediaFormatter : MediaTypeFormatter
{
private const string Atom = "application/atom+xml";
public AtomPubMediaFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue(Atom));
this.AddQueryStringMapping("format", "atom", Atom);
}
@benfoster
benfoster / gist:3610765
Created September 3, 2012 16:51
A production Web Api Controller
/// <summary>
/// Page Resource API.
/// </summary>
public class PagesController : ApiControllerBase
{
/// <summary>
/// Get pages from the specified site.
/// </summary>
/// <param name="siteId">The site identifier.</param>
/// <param name="command">A command containing result paging parameters.</param>
@benfoster
benfoster / gist:3635064
Created September 5, 2012 11:05
Testing route generation
var config = new HttpConfiguration();
config.Routes.MapHttpRoute("Foo", "foo/{id}", new { controller = "foo" });
config.Routes.MapHttpRoute("Bar", "bar/{id}", new { controller = "bar" });
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "/");
var vp = config.Routes.GetVirtualPath(requestMessage, "Foo" , new Dictionary<string,object> { { "controller", "foo" }, { "id", 10 } });
Console.WriteLine(vp);
Console.ReadLine();
@benfoster
benfoster / gist:3635702
Created September 5, 2012 12:09
Link generation without specifying route name in ASP.NET Web API
public static class UrlHelperExtensions
{
public static string Link(this UrlHelper urlHelper, object values)
{
var config = urlHelper.Request.GetConfiguration();
var routeValues = new HttpRouteValueDictionary(values);
routeValues.Add("httproute", true); // required to define a web api route