/// <summary> /// Registers the HTTP routes. /// </summary> public static void RegisterRoutes(HttpRouteCollection httpRouteCollection) { // Build the HTTP method contraint. HttpMethodConstraint httpMethodConstraint = new HttpMethodConstraint(HttpMethod.Post); // Map the HTTP route. httpRouteCollection.MapHttpRoute( name: "api/scheduling/course-schedules", routeTemplate: "api/scheduling/course-schedules", defaults: new { controller = "CourseSchedules" }, constraints: new { httpMethods = httpMethodConstraint }); }
/// <summary> /// Builds an <see cref="IHttpRoute"/> for a particular action. /// </summary> /// <param name="routeTemplate">The tokenized route template for the route.</param> /// <param name="httpMethods">The HTTP methods supported by the route.</param> /// <param name="actions">The actions to invoke for the route.</param> /// <returns>The generated <see cref="IHttpRoute"/>.</returns> public virtual IHttpRoute BuildHttpRoute( string routeTemplate, IEnumerable<HttpMethod> httpMethods, IEnumerable<ReflectedHttpActionDescriptor> actions) { if (routeTemplate == null) { throw Error.ArgumentNull("routeTemplate"); } HttpRouteValueDictionary defaults = new HttpRouteValueDictionary(); HttpRouteValueDictionary constraints = new HttpRouteValueDictionary(); if (httpMethods != null) { // Current method constraint implementation is inefficient since it matches before running the constraint. // Consider checking the HTTP method first in a custom route as a performance optimization. constraints["httpMethod"] = new HttpMethodConstraint(httpMethods.AsArray()); } string detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver); return BuildHttpRoute(detokenizedRouteTemplate, defaults, constraints, actions); }
public IEnumerable<UriResolutionResult> Get() { string routeTemplate = "movies/{genre}/{title}/{id}"; IHttpRoute route = new HttpRoute(routeTemplate); IHttpRouteConstraint contraint = new HttpMethodConstraint(HttpMethod.Post); route.Constraints.Add("httpMethod", contraint); string requestUri = "http://www.artech.com/api/movies/romance/titanic/001"; HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Get, requestUri); HttpRequestMessage request2 = new HttpRequestMessage(HttpMethod.Post, requestUri); string root1 = "/"; string root2 = "/api/"; IHttpRouteData routeData1 = route.GetRouteData(root1, request1); IHttpRouteData routeData2 = route.GetRouteData(root1, request2); IHttpRouteData routeData3 = route.GetRouteData(root2, request1); IHttpRouteData routeData4 = route.GetRouteData(root2, request2); yield return new UriResolutionResult(root1,"GET", routeData1 != null); yield return new UriResolutionResult(root1,"POST", routeData2 != null); yield return new UriResolutionResult(root2,"GET", routeData3 != null); yield return new UriResolutionResult(root2, "POST", routeData4 != null); }
/// <summary> /// Builds an <see cref="IHttpRoute"/> for a particular action. /// </summary> /// <param name="routeTemplate">The tokenized route template for the route.</param> /// <param name="httpMethods">The HTTP methods supported by the route.</param> /// <param name="actions">The actions to invoke for the route.</param> /// <returns>The generated <see cref="IHttpRoute"/>.</returns> public virtual IHttpRoute BuildHttpRoute( string routeTemplate, IEnumerable <HttpMethod> httpMethods, IEnumerable <ReflectedHttpActionDescriptor> actions) { if (routeTemplate == null) { throw Error.ArgumentNull("routeTemplate"); } HttpRouteValueDictionary defaults = new HttpRouteValueDictionary(); HttpRouteValueDictionary constraints = new HttpRouteValueDictionary(); if (httpMethods != null) { // Current method constraint implementation is inefficient since it matches before running the constraint. // Consider checking the HTTP method first in a custom route as a performance optimization. constraints["httpMethod"] = new HttpMethodConstraint(httpMethods.AsArray()); } string detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver); return(BuildHttpRoute(detokenizedRouteTemplate, defaults, constraints, actions)); }