Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Last active December 28, 2015 23:49
Show Gist options
  • Select an option

  • Save beyond-code-github/7581816 to your computer and use it in GitHub Desktop.

Select an option

Save beyond-code-github/7581816 to your computer and use it in GitHub Desktop.
public class MailController : ApiController
{
private readonly dynamic[] mail =
{
new { Id = 1, Location = "Inbox", Subject = "value1" },
new { Id = 2, Location = "Inbox", Subject = "value2" },
new { Id = 3, Location = "Inbox", Subject = "value3" },
new { Id = 4, Location = "Deleted", Subject = "value4" },
new { Id = 5, Location = "Sent", Subject = "value5" }
};
public IEnumerable<object> Get()
{
return mail;
}
public IEnumerable<object> Get(string location)
{
return mail.Where(o => o.Location == location);
}
public object GetById(int id)
{
return mail.FirstOrDefault(o => o.Id == id);
}
}
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Routes.MapHttpRoute(
name: "Mail by Location",
routeTemplate: "api/mail/{location}",
defaults: new { controller = "Mail" },
constraints: new { location = @"[a-zA-Z]+" }
);
config.Routes.MapHttpRoute(
name: "Mail",
routeTemplate: "api/mail/{id}",
defaults: new { controller = "Mail", id = RouteParameter.Optional }
);
/api/mail
/api/mail/3
/api/mail/inbox
/api/mail/sent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment