Skip to content

Instantly share code, notes, and snippets.

@karbyninc
Last active February 11, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karbyninc/40879a6e7c0ef5f8a0e5 to your computer and use it in GitHub Desktop.
Save karbyninc/40879a6e7c0ef5f8a0e5 to your computer and use it in GitHub Desktop.
public IQueryable<CustomerPurchases> GetPurchases(int customerID)
{
return _purchaseService.GetPurchases(customerID);
}
public class CouponCodeConstraint : IHttpRouteConstraint
{
public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName,
IDictionary&lt;string, object&gt; values, HttpRouteDirection routeDirection)
{
object o;
if (values.TryGetValue(parameterName, out o) &amp;&amp; o != null)
{
if (!values.TryGetValue(parameterName, out o) || o == null)
return false;
///Coupon Code must be in format c#####c, i.e. A12345D
return (Regex.Match(Convert.ToString(o), "^[a-zA-Z][0-9]{5}[a-zA-Z]$").Success);
}
return false;
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var constraintResolver = new DefaultInlineConstraintResolver();
constraintResolver.ConstraintMap.Add("coupon", typeof(CouponCodeConstraint));
// Web API routes
config.MapHttpAttributeRoutes(constraintResolver);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
[HttpGet, Route("{couponCode:coupon}")]
public HttpResponseMessage GetCoupon(String couponCode)
{
...
}
[HttpGet, Route("api/customer/{customerID}/purchases")]
[HttpGet, Route("api/customer/{customerID}/purchases")]
public IQueryable<CustomerPurchases> GetPurchases(int customerID)
{
return _purchaseService.GetPurchases(customerID);
}
[RoutePrefix("api/customer")]
public class CustomerController : ApiController
[HttpGet, Route("{customerID}")]
[HttpGet, Route("api/customer/{customerID:guid}/purchases")]
[Route("api/pages/{pageNum:int=1}")]
[Route("api/pages/{pageNum:int?}")]
[Route("{customerID:minlength(1):alpha}")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment