public void add_constraint_to_route_with_model()
        {
            var url = new RouteDefinition<SampleViewModel>("my/sample");
            var constraintToAdd = new HttpMethodConstraint("POST");
            url.AddRouteConstraint("httpMethod", constraintToAdd);
            Route route = url.ToRoute();

            route.Constraints["httpMethod"].ShouldEqual(constraintToAdd);
        }
        public void add_constraint_to_route_with_model()
        {
            var parent = new RouteDefinition("my/sample");
            parent.Input = new RouteInput<SampleViewModel>(parent);
            var constraintToAdd = new HttpMethodConstraint("POST");
            parent.AddRouteConstraint("httpMethod", constraintToAdd);
            Route route = parent.ToRoute();

            route.Constraints["httpMethod"].ShouldEqual(constraintToAdd);
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // configure HttpHandler for serving static images
            HttpMethodConstraint GetFileMethodConstraints = new HttpMethodConstraint(new string[]{ "GET" });
            Route GetFileRoute = new Route(
                url: "Files/{id}/{filename}",
                routeHandler: new DobImageRouteHandler(),
                defaults: null,
                constraints: new RouteValueDictionary { { "httpMethod", GetFileMethodConstraints } }
            );
            routes.Add("GetFileRoute", GetFileRoute);


            //routes.MapRoute(
            //    name: "Get File",
            //    url: "Files/{id}/{filename}",
            //    defaults: new { controller = "Files", action = "Find" },
            //    constraints: new { httpMethod = new HttpMethodConstraint("GET") }
            //);

            routes.MapRoute(
                name: "Delete File",
                url: "Files/{id}/{filename}",
                defaults: new { controller = "Files", action = "Delete" },
                constraints: new { httpMethod = new HttpMethodConstraint("DELETE") }
            );

            routes.MapRoute(
                name: "Get Json File List",
                url: "Files",
                defaults: new { controller = "Files", action = "List" },
                constraints: new { httpMethod = new HttpMethodConstraint("GET") }
            );

            routes.MapRoute(
                name: "Post Files",
                url: "Files",
                defaults: new { controller = "Files", action = "Uploads" },
                constraints: new { httpMethod = new HttpMethodConstraint("POST") }
            );


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
 static void ApplyConstraints(IEnumerable<string> args,RouteBuilderInfo info,IDictionary<string,object> constraints)
 {
     var httpMethod = info.ActionCall.Method.Name.StartsWith("post",
                                                   StringComparison.OrdinalIgnoreCase)
                         ? "POST"
                         : "GET";
     constraints["httpMethod"] = new HttpMethodConstraint(httpMethod);
     foreach (var arg in args)
     {
         var param = info.ActionCall.GetActionArgument(arg);
         if (param != null)
         {
             var constraint = info.GetConstraint(param.ParameterType);
             if (constraint != null)
             {
                 constraints[arg] = constraint;
             }
         }
     }
 }
		private void RegisterRoute(string area, string controller, XElement route)
		{
			XAttribute action = route.RequiredAttribute("action");
			XAttribute pattern = route.RequiredAttribute("pattern");
			XAttribute verbs = route.Attribute("verbs");

			var defaults = new RouteValueDictionary();
			var constraints = new RouteValueDictionary();

			defaults["area"] = area;
			defaults["controller"] = controller;
			defaults["action"] = action.Value;

			if (verbs != null && !String.IsNullOrEmpty(verbs.Value) && !verbs.Value.Equals("*"))
			{
				string[] tokens = verbs.Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
				constraints["httpMethod"] = new HttpMethodConstraint(tokens.Select(m => m.ToUpperInvariant()).ToArray());
			}

			string routeName = controller + "." + action.Value;
			Routes.Add(routeName, new Route(pattern.Value, defaults, constraints, new MvcRouteHandler()));
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="HostIndependentHttpMethodConstraint"/> class.
 /// </summary>
 /// <param name="method">The method.</param>
 public HostIndependentHttpMethodConstraint(string method)
 {
     aspNetConstraint = new HttpMethodConstraint(method);
     selfHostConstraint = new global::System.Web.Http.Routing.HttpMethodConstraint(new HttpMethod(method));
 }
Exemple #7
0
        public void MapRoute(string routeName, string url, object defaults = null, object constraints = null)
        {
            string[] stems = routeName.Split('_');
            int controllerIndex = 0;
            int actionIndex = 1;

            RouteValueDictionary rvd = null;
            RouteValueDictionary cvd = null;

            if (defaults != null)
            {
                rvd = new RouteValueDictionary(defaults);
            }

            if (constraints != null)
            {
                cvd = new RouteValueDictionary(constraints);
            }

            if (stems.Length == 3)
            {
                controllerIndex = 1;
                actionIndex = 2;

                HttpMethodConstraint httpMethod =  new HttpMethodConstraint("GET");

                if (stems[0] == "post") httpMethod =  new HttpMethodConstraint("POST");
                else if (stems[0] == "put") httpMethod =  new HttpMethodConstraint("PUT");
                else if (stems[0] == "delete") httpMethod = new HttpMethodConstraint("DELETE");

                if (constraints == null)
                {
                    constraints = new { httpMethod = httpMethod };
                    cvd = new RouteValueDictionary(constraints);
                }
                else
                {
                    cvd["httpMethod"] = httpMethod;
                }
            }

            if (defaults == null)
            {
                if (constraints == null)
                {
                    _routes.MapRoute(routeName, url, new { controller = stems[controllerIndex].Camelize(), action = stems[actionIndex] });
                }
                else
                {
                   _routes.Add(routeName, new Route(url, new RouteValueDictionary(new { controller = stems[controllerIndex].Camelize(), action = stems[actionIndex] }), cvd, new MvcRouteHandler()));
                }
            }
            else
            {
                rvd["controller"] = stems[controllerIndex].Camelize();
                rvd["action"] = stems[actionIndex].Camelize();

                if (constraints == null)
                {
                    _routes.Add(routeName, new Route(url, rvd, new MvcRouteHandler()));
                }
                else
                {
                    _routes.Add(routeName, new Route(url, rvd, cvd, new MvcRouteHandler()));
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="HostIndependentHttpMethodConstraint"/> class.
 /// </summary>
 /// <param name="method">The method.</param>
 public HostIndependentHttpMethodConstraint(string method)
 {
     aspNetConstraint = new HttpMethodConstraint(method);
     selfHostConstraint = new global::System.Web.Http.Routing.HttpMethodConstraint(new HttpMethod(method));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="HostIndependentHttpMethodConstraint"/> class.
 /// </summary>
 /// <param name="method">The method.</param>
 public HostIndependentHttpMethodConstraint(string method)
 {
     aspNetConstraint   = new HttpMethodConstraint(method);
     selfHostConstraint = new Routing.HttpMethodConstraint(new HttpMethod(method));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="HostIndependentHttpMethodConstraint"/> class.
 /// </summary>
 /// <param name="method">The method.</param>
 public HostIndependentHttpMethodConstraint(string method)
 {
     aspNetConstraint = new HttpMethodConstraint(method);
     selfHostConstraint = new Routing.HttpMethodConstraint(new HttpMethod(method));
 }
		public void UrlGeneration ()
		{
			var c = new HttpMethodConstraint (new string[] { "GET" }) as IRouteConstraint;
			var req = new HttpContextStub ("", "", "HEAD");

			var values = new RouteValueDictionary () { { "httpMethod", "GET" } };
			Assert.IsTrue (c.Match (req, new Route (null, null), "httpMethod", values, RouteDirection.UrlGeneration), "#1");

			values = new RouteValueDictionary() { { "httpMethod", "POST" } };
			Assert.IsFalse (c.Match (req, new Route (null, null), "httpMethod", values, RouteDirection.UrlGeneration), "#2");
		}
Exemple #12
0
        public static void RegisterRoutes(RouteCollection routes)
        {
            HttpMethodConstraint methodConstraintsGet = new HttpMethodConstraint(new string[]{ "GET" });
            HttpMethodConstraint methodConstraintsPost = new HttpMethodConstraint(new string[] { "POST" });
            HttpMethodConstraint methodConstraintsDelete = new HttpMethodConstraint(new string[] { "DELETE" });
            HttpMethodConstraint methodConstraintsPut = new HttpMethodConstraint(new string[] { "PUT" });
            HttpMethodConstraint methodConstraintsOptions = new HttpMethodConstraint(new string[] { "OPTIONS" });

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //------------------------------------------------------------
            // HTTP OPTIONS route
            routes.MapRoute(
                "OPTIONS",
                "{clazz}/{id}",
                new {
                    controller = "HttpRequest",
                    action = "optionsRequest",
                    id = UrlParameter.Optional
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsOptions }
                };
            routes.MapRoute(
                "OPTIONS2",
                "{clazz}/{id}/{requestedAction}",
                new
                {
                    controller = "HttpRequest",
                    action = "optionsRequest",
                    id = UrlParameter.Optional
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsOptions }
                };

            //------------------------------------------------------------
            // Pilot routes
            routes.MapRoute("GetPilotsList",
                "pilots",
                new {
                    controller = "PilotAccount",
                    action = "index"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet }
                };

            routes.MapRoute("CreatePilot",
                "pilot",
                new
                {
                    controller = "PilotAccount",
                    action = "create"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPost }
                };

            routes.MapRoute("ReadPilot",
                "pilot/{id}",
                new
                {
                    controller = "PilotAccount",
                    action = "read"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdatePilot",
                "pilot/{id}",
                new {
                    controller = "PilotAccount",
                    action = "update"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdatePilotPassword",
                "pilot/{id}/updatePassword",
                new
                {
                    controller = "PilotAccount",
                    action = "updatePassword"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("DeletePilot",
                "pilot/{id}",
                new
                {
                    controller = "PilotAccount",
                    action = "delete"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsDelete },
                    { "id", @"\d+"}
                };

            routes.MapRoute("CreditAccountPilot",
                "pilot/{id}/credit",
                new
                {
                    controller = "PilotAccount",
                    action = "creditPilotAccount"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            //------------------------------------------------------------
            // Create AirBase Manager routes
            routes.MapRoute("GetAirBaseManagerList",
                "airbaseManagers",
                new
                {
                    controller = "AirbaseManager",
                    action = "getAirbaseManagers"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet }
                };

            routes.MapRoute("CreateAirbaseManager",
                "airbaseManager",
                new
                {
                    controller = "AirbaseManager",
                    action = "create"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPost }
                };

            routes.MapRoute("ReadAirbaseManager",
                "airbaseManager/{id}",
                new
                {
                    controller = "AirbaseManager",
                    action = "read"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateAirbaseManager",
                "airbaseManager/{id}",
                new
                {
                    controller = "AirbaseManager",
                    action = "update"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateAirbaseManagerPassword",
                "airbaseManager/{id}/updatePassword",
                new
                {
                    controller = "AirbaseManager",
                    action = "updatePassword"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("DeleteAirbaseManager",
                "airbaseManager/{id}",
                new
                {
                    controller = "AirbaseManager",
                    action = "delete"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsDelete },
                    { "id", @"\d+"}
                };

            //------------------------------------------------------------
            // Create SuperAdmin routes
            routes.MapRoute("GetSuperAdmin",
                "superAdmins",
                new
                {
                    controller = "SuperAdmin",
                    action = "index"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet }
                };

            routes.MapRoute("CreateSuperAdmin",
                "superAdmin",
                new
                {
                    controller = "SuperAdmin",
                    action = "create"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPost }
                };

            routes.MapRoute("ReadSuperAdmin",
                "superAdmin/{id}",
                new
                {
                    controller = "SuperAdmin",
                    action = "read"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateSuperAdmin",
                "superAdmin/{id}",
                new
                {
                    controller = "SuperAdmin",
                    action = "update"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateSuperAdminPassword",
                "superAdmin/{id}/updatePassword",
                new
                {
                    controller = "SuperAdmin",
                    action = "updatePassword"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            //------------------------------------------------------------
            // Create Secretary routes
            routes.MapRoute("GetSecretariesList",
                "secretaries",
                new
                {
                    controller = "Secretary",
                    action = "index"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet }
                };

            routes.MapRoute("CreateSecretary",
                "secretary",
                new
                {
                    controller = "Secretary",
                    action = "create"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPost }
                };

            routes.MapRoute("ReadSecretary",
                "secretary/{id}",
                new
                {
                    controller = "Secretary",
                    action = "read"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateSecretary",
                "secretary/{id}",
                new
                {
                    controller = "Secretary",
                    action = "update"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateSecretaryPassword",
                "secretary/{id}/updatePassword",
                new
                {
                    controller = "Secretary",
                    action = "updatePassword"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            //------------------------------------------------------------
            // Auth routes
            routes.MapRoute("Login",
                "login",
                new
                {
                    controller = "Auth",
                    action = "login"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPost }
                };
            routes.MapRoute("Logout",
                "logout",
                new
                {
                    controller = "Auth",
                    action = "logout"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet }
                };

            //------------------------------------------------------------
            // Airbase routes
            routes.MapRoute("GetAirbasesList",
                "airbases",
                 new
                 {
               controller = "Airbase",
               action = "index"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet }
                };

            routes.MapRoute("airbasesListByManager",
                "airbasesByManager/{id}",
                 new
                 {
                     controller = "Airbase",
                     action = "airbasesListByManager"
                 }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet }
                };

            routes.MapRoute("CreateAirbase",
                "airbase",
                new
                {
                    controller = "Airbase",
                    action = "create"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPost }
                };

            routes.MapRoute("ReadAirbase",
                "airbase/{id}",
                new
                {
                    controller = "Airbase",
                    action = "read"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateAirbase",
                "airbase/{id}",
                new
                {
                    controller = "Airbase",
                    action = "update"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("DeleteAirbase",
                "airbase/{id}",
                new
                {
                    controller = "Airbase",
                    action = "delete"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsDelete },
                    { "id", @"\d+"}
                };

            //------------------------------------------------------------
            // Transaction routes
            routes.MapRoute("CreateTransaction",
                "transaction",
                new
                {
                    controller = "Transaction",
                    action = "doTransaction"
                }
            ).Constraints = new RouteValueDictionary { { "httpMethod", methodConstraintsPost } };

            routes.MapRoute("GetTransactionByPilotIDList",
               "getTransactionByPilotID",
               new
               {
                   controller = "Transaction",
                   action = "getTransactionByPilotID"
               }
               ).Constraints = new RouteValueDictionary { { "httpMethod", methodConstraintsGet } };
            routes.MapRoute("ReadTransaction",
               "transaction/{id}",
               new
               {
                   controller = "Transaction",
                   action = "read"
               }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("TransactionServices",
                "transaction/services",
                new
                {
                    controller = "Transaction",
                    action = "services"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPost }
                };
            //------------------------------------------------------------
            // Services Routes

            routes.MapRoute("GetServicesList",
                "services",
                 new
                 {
                     controller = "Service",
                     action = "index"
                 }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet }
                };

            routes.MapRoute("CreateService",
                "service",
                new
                {
                    controller = "Service",
                    action = "create"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPost }
                };

            routes.MapRoute("serviceListByAirbase",
               "servicesByAirbase/{id}",
               new
               {
                   controller = "Service",
                   action = "readByAirbase"
               }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("GetService",
                "service/{id}",
                new
                {
                    controller = "Service",
                    action = "read"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateService",
                "service/{id}",
                new
                {
                    controller = "Service",
                    action = "update"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("DeleteService",
                "service/{id}",
                 new
                {
                    controller = "Service",
                    action = "delete"
                }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsDelete },
                    { "id", @"\d+"}
                };

                //WeightRangeServiceRoute
            routes.MapRoute("CreateWeightRangeService",
                "weightRangeService",
                new
                {
                    controller = "WeightRangeService",
                    action = "create"
                }
                ).Constraints = new RouteValueDictionary { { "httpMethod", methodConstraintsPost } };

            routes.MapRoute("ReadWeightRangeService",
                "weightRangeService/{id}",
                new
               {
                   controller = "WeightRangeService",
                   action = "read"
               }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsGet },
                    { "id", @"\d+"}
                };

            routes.MapRoute("UpdateWeightRangeService",
               "weightRangeService/{id}",
               new
               {
                   controller = "WeightRangeService",
                   action = "update"
               }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsPut },
                    { "id", @"\d+"}
                };

            routes.MapRoute("DeleteWeightRangeService",
               "weightRangeService/{id}",
               new
               {
                   controller = "WeightRangeService",
                   action = "delete"
               }).Constraints = new RouteValueDictionary {
                    { "httpMethod", methodConstraintsDelete},
                    { "id", @"\d+"}
                };

            //------------------------------------------------------------
            //Default route
            routes.MapRoute(
                "Default", // Nom d'itinéraire
                "{controller}/{action}/{id}", // URL avec des paramètres
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Paramètres par défaut
            );
        }