Ejemplo n.º 1
0
        public void Apply(ApplicationModel application)
        {
            var controllers = application.Controllers
                              .Where(c => _controllerTypes.Contains(c.ControllerType));

            foreach (var controller in controllers)
            {
                var matchedSelectors = controller.Selectors
                                       .Where(x => x.AttributeRouteModel != null)
                                       .ToList();
                if (matchedSelectors.Any())
                {
                    foreach (var selectorModel in matchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = AttributeRouteModel
                                                            .CombineAttributeRouteModel(_routePrefix, selectorModel.AttributeRouteModel);
                    }
                }

                var unmatchedSelectors = controller.Selectors
                                         .Where(x => x.AttributeRouteModel == null)
                                         .ToList();
                if (unmatchedSelectors.Any())
                {
                    foreach (var selectorModel in unmatchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = _routePrefix;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Apply(ApplicationModel Application)
        {
            foreach (var controller in Application.Controllers)
            {
                var matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList();
                if (matchedSelectors.Any())
                {
                    foreach (var selectorModel in matchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_centralPrefix,
                                                                                                           selectorModel.AttributeRouteModel);
                    }
                }

                var unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList();

                if (unmatchedSelectors.Any())
                {
                    foreach (var selectorModel in unmatchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = _centralPrefix;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        //接口的Apply方法
        public void Apply(ApplicationModel application)
        {
            //遍历所有的 Controller
            foreach (var controller in application.Controllers)
            {
                // 已经标记了 RouteAttribute 的 Controller
                var matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList();
                if (matchedSelectors.Any())
                {
                    foreach (var selectorModel in matchedSelectors)
                    {
                        // 在 当前路由上 再 添加一个 路由前缀
                        selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_centralPrefix,
                                                                                                           selectorModel.AttributeRouteModel);
                    }
                }

                // 没有标记 RouteAttribute 的 Controller
                var unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList();
                if (unmatchedSelectors.Any())
                {
                    foreach (var selectorModel in unmatchedSelectors)
                    {
                        // 添加一个 路由前缀
                        selectorModel.AttributeRouteModel = _centralPrefix;
                    }
                }
            }
        }
        public void Apply(ControllerModel controller)
        {
            //var ns = controller.ControllerType.Assembly.GetName().Name;
            var    ns     = Path.GetFileNameWithoutExtension(controller.ControllerType.Assembly.ManifestModule.Name);
            string prefix = Modularity.GetControllerRoutePrefix(ns, controller.Attributes);

            if (!String.IsNullOrWhiteSpace(prefix))
            {
                var _routePrefix = new AttributeRouteModel(new RouteAttribute(prefix));

                foreach (var selector in controller.Selectors)
                {
                    //tanımlanmış bir route attribute var ..onunla cobmine ediyoruz..
                    if (selector.AttributeRouteModel != null)
                    {
                        selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_routePrefix, selector.AttributeRouteModel);
                    }
                    else
                    {
                        //yoksada direkt olarak ekliyoruz.
                        selector.AttributeRouteModel = _routePrefix;
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public void Apply(ApplicationModel application)
        {
            foreach (var controller in application.Controllers)
            {
                var routeSelectors = controller.Selectors.Where(s => s.AttributeRouteModel != null).ToList();
                if (routeSelectors.Any())
                {
                    foreach (var selector in routeSelectors)
                    {
                        selector.AttributeRouteModel =
                            AttributeRouteModel.CombineAttributeRouteModel(_centralPrefix,
                                                                           selector.AttributeRouteModel);
                    }
                }

                var unRouteSelectors = controller.Selectors.Where(s => s.AttributeRouteModel == null).ToList();
                if (unRouteSelectors.Any())
                {
                    foreach (var selector in unRouteSelectors)
                    {
                        selector.AttributeRouteModel = _centralPrefix;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void Apply(ApplicationModel application)
        {
            foreach (ControllerModel controller in application.Controllers)
            {
                List <SelectorModel> _matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList();
                if (_matchedSelectors.Any())
                {
                    foreach (SelectorModel selectorModel in _matchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_centralPrefix,
                                                                                                           selectorModel.AttributeRouteModel);
                    }
                }

                List <SelectorModel> _unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList();

                if (_unmatchedSelectors.Any())
                {
                    foreach (SelectorModel selectorModel in _unmatchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = _centralPrefix;
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public void Apply(ApplicationModel application)
 {
     foreach (var selector in application.Controllers.SelectMany(c => c.Selectors))
     {
         selector.AttributeRouteModel = selector.AttributeRouteModel != null?AttributeRouteModel.CombineAttributeRouteModel(_routePrefix, selector.AttributeRouteModel) : this._routePrefix;
     }
 }
        public void Apply(ApplicationModel application)
        {
            foreach (var component in _components)
            {
                var typeNames = component.ControllerTypes.Select(x =>
                                                                 x.Name.Contains('`') ? x.Name : x.Name.Replace(nameof(Controller), string.Empty)).ToList();

                foreach (var controller in application.Controllers)
                {
                    if (!typeNames.Contains(controller.ControllerName))
                    {
                        continue;
                    }

                    var template = component.Namespace();
                    var prefix   = new AttributeRouteModel(new RouteAttribute(template));

                    foreach (var selector in controller.Selectors)
                    {
                        selector.AttributeRouteModel = selector.AttributeRouteModel != null
                            ? AttributeRouteModel.CombineAttributeRouteModel(prefix, selector.AttributeRouteModel)
                            : prefix;
                    }
                }
            }
        }
Ejemplo n.º 9
0
 public void Apply(ApplicationModel application)
 {
     foreach (var selector in application.Controllers.SelectMany(i => i.Selectors))
     {
         selector.AttributeRouteModel = selector.AttributeRouteModel != null
              ? AttributeRouteModel.CombineAttributeRouteModel(_CentralPrefix, selector.AttributeRouteModel)
             : _CentralPrefix;
     }
 }
Ejemplo n.º 10
0
 private static void NormalizeSelectorRoutes(string areaName, string controllerName, ActionModel action)
 {
     foreach (var selector in action.Selectors)
     {
         selector.AttributeRouteModel = selector.AttributeRouteModel == null?
                                        CreateActionRouteModel(areaName, controllerName, action) :
                                            AttributeRouteModel.CombineAttributeRouteModel(CreateActionRouteModel(areaName, controllerName, action), selector.AttributeRouteModel);
     }
 }
Ejemplo n.º 11
0
    public void Combine_NullOrNullTemplateReflectedAttributeRouteModels(
        AttributeRouteModel left,
        AttributeRouteModel right)
    {
        // Arrange & Act
        var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right);

        // Assert
        Assert.Null(combined);
    }
Ejemplo n.º 12
0
 /// <summary>
 /// 生成每个路由URL;
 /// 在原来路由上添加
 /// </summary>
 /// <param name="areaName"></param>
 /// <param name="controllerName"></param>
 /// <param name="action"></param>
 private static void NormalizeSelectorRoutes(string version, string areaName, string controllerName, ActionModel action)
 {
     action.ActionName = GetRestFulActionName(action.ActionName);
     foreach (var selector in action.Selectors)
     {
         selector.AttributeRouteModel = selector.AttributeRouteModel == null?
                                        CreateActionRouteModel(version, areaName, controllerName, action.ActionName) :
                                            AttributeRouteModel.CombineAttributeRouteModel(CreateActionRouteModel(version, areaName, controllerName, ""), selector.AttributeRouteModel);
     }
 }
 /// <summary>
 /// 添加前缀当已经定义路由
 /// </summary>
 /// <param name="controller"></param>
 private void AddPrefixesToExistingRoutes(ControllerModel controller)
 {
     foreach (var selectorModel in controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList())
     {
         // Merge existing route models with the api prefix
         var originalAttributeRoute = selectorModel.AttributeRouteModel;
         selectorModel.AttributeRouteModel =
             AttributeRouteModel.CombineAttributeRouteModel(onlyPrefixRoute, originalAttributeRoute);
     }
 }
        public void Apply(ApplicationModel application)
        {
            var culturePrefix = new AttributeRouteModel(new RouteAttribute("{culture" + (!string.IsNullOrEmpty(_cultureConstraintKey) ? $":{_cultureConstraintKey}" : "") + "}"));

            foreach (var controller in application.Controllers)
            {
                var newSelectors = new List <SelectorModel>();

                var matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList();
                if (matchedSelectors.Any())
                {
                    foreach (var selectorModel in matchedSelectors)
                    {
                        var routeModel = AttributeRouteModel.CombineAttributeRouteModel(culturePrefix, selectorModel.AttributeRouteModel);

                        if (_optional)
                        {
                            var newSelector = new SelectorModel();
                            newSelector.AttributeRouteModel       = routeModel;
                            newSelector.AttributeRouteModel.Order = -1;
                            newSelectors.Add(newSelector);
                        }
                        else
                        {
                            selectorModel.AttributeRouteModel = routeModel;
                        }
                    }
                }

                var unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList();
                if (unmatchedSelectors.Any())
                {
                    foreach (var selectorModel in unmatchedSelectors)
                    {
                        var routeModel = culturePrefix;
                        if (_optional)
                        {
                            var newSelector = new SelectorModel();
                            newSelector.AttributeRouteModel       = routeModel;
                            newSelector.AttributeRouteModel.Order = -1;
                            newSelectors.Add(newSelector);
                        }
                        else
                        {
                            selectorModel.AttributeRouteModel = routeModel;
                        }
                    }
                }

                foreach (var newSelector in newSelectors)
                {
                    controller.Selectors.Insert(0, newSelector);
                }
            }
        }
Ejemplo n.º 15
0
        public void Apply(ApplicationModel application)
        {
            var prefix = new AttributeRouteModel(new RouteAttribute("api"));

            foreach (var selector in application.Controllers.SelectMany(x => x.Selectors))
            {
                selector.AttributeRouteModel = selector.AttributeRouteModel != null
                    ? AttributeRouteModel.CombineAttributeRouteModel(prefix, selector.AttributeRouteModel)
                    : prefix;
            }
        }
Ejemplo n.º 16
0
 public void Apply(ApplicationModel application)
 {
     foreach (ControllerModel controller in application.Controllers
              .Where(c => c.ControllerType.Namespace == "SIL.Machine.WebApi.Controllers"))
     {
         SelectorModel selector = controller.Selectors.First(sm => sm.AttributeRouteModel != null);
         selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_prefixRoute,
                                                                                       selector.AttributeRouteModel);
         controller.Filters.Add(_authorizeFilter);
     }
 }
Ejemplo n.º 17
0
    public void Combine_Names(
        AttributeRouteModel left,
        AttributeRouteModel right,
        string expectedName)
    {
        // Arrange & Act
        var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right);

        // Assert
        Assert.NotNull(combined);
        Assert.Equal(expectedName, combined.Name);
    }
Ejemplo n.º 18
0
 public void Apply(ApplicationModel application)
 {
     foreach (var controller in application.Controllers)
     {
         foreach (var selectorModel in controller.Selectors)
         {
             selectorModel.AttributeRouteModel = selectorModel.AttributeRouteModel != null
                 ? AttributeRouteModel.CombineAttributeRouteModel(_centralPrefix, selectorModel.AttributeRouteModel)
                 : _centralPrefix;
         }
     }
 }
Ejemplo n.º 19
0
    public void Combine_Orders(
        AttributeRouteModel left,
        AttributeRouteModel right,
        int?expected)
    {
        // Arrange & Act
        var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right);

        // Assert
        Assert.NotNull(combined);
        Assert.Equal(expected, combined.Order);
    }
Ejemplo n.º 20
0
    public void Combine_ValidReflectedAttributeRouteModels(
        AttributeRouteModel left,
        AttributeRouteModel right,
        AttributeRouteModel expectedResult)
    {
        // Arrange & Act
        var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right);

        // Assert
        Assert.NotNull(combined);
        Assert.Equal(expectedResult.Template, combined.Template);
    }
Ejemplo n.º 21
0
    public void Combine_SetsSuppressPathGenerationToFalse_IfNeitherIsTrue()
    {
        // Arrange
        var left = new AttributeRouteModel
        {
            Template = "Template",
        };
        var right    = new AttributeRouteModel();
        var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right);

        // Assert
        Assert.False(combined.SuppressPathMatching);
    }
Ejemplo n.º 22
0
 public void Apply(ApplicationModel applicationModel)
 {
     foreach (var controller in applicationModel.Controllers.SelectMany(x => x.Selectors))
     {
         if (controller.AttributeRouteModel != null)
         {
             controller.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_attributeRouteModel, controller.AttributeRouteModel);
         }
         else
         {
             controller.AttributeRouteModel = _attributeRouteModel;
         }
     }
 }
Ejemplo n.º 23
0
        private void AddPrefixesToExistingRoutes(ControllerModel controller)
        {
            foreach (var selectorModel in controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList())
            {
                var originalAttributeRoute = selectorModel.AttributeRouteModel;
                // Merge controller selector with the api prefix
                selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(apiPrefix, originalAttributeRoute);

                // Add another selector with the culture api prefix
                var cultureSelector = new SelectorModel(selectorModel);
                cultureSelector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(apiCulturePrefix, originalAttributeRoute);
                controller.Selectors.Add(cultureSelector);
            }
        }
Ejemplo n.º 24
0
 public void Apply(ApplicationModel application)
 {
     foreach (SelectorModel selector in application.Controllers.SelectMany(c => c.Selectors))
     {
         if (selector.AttributeRouteModel != null)
         {
             selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_routePrefix, selector.AttributeRouteModel);
         }
         else
         {
             selector.AttributeRouteModel = _routePrefix;
         }
     }
 }
 /// <summary>
 /// Add {culture?} route segment
 /// </summary>
 /// <param name="application"></param>
 public void Apply(ApplicationModel application)
 {
     foreach (var selector in application.Controllers.SelectMany(c => c.Selectors))
     {
         if (selector.AttributeRouteModel != null)
         {
             selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_culture, selector.AttributeRouteModel);
         }
         else
         {
             selector.AttributeRouteModel = _culture;
         }
     }
 }
Ejemplo n.º 26
0
 public void Apply(ControllerModel controller)
 {
     foreach (var selector in controller.Selectors.Safe())
     {
         if (selector.AttributeRouteModel != null)
         {
             selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(this.routePrefix, selector.AttributeRouteModel);
         }
         else
         {
             selector.AttributeRouteModel = this.routePrefix;
         }
     }
 }
Ejemplo n.º 27
0
            public void Apply(ApplicationModel application)
            {
                var matchedSelectors = application.Controllers.FirstOrDefault(c => c.ControllerType == typeof(T))?.Selectors;

                if (matchedSelectors != null && matchedSelectors.Any())
                {
                    var centralPrefix = new AttributeRouteModel(_routeTemplateProvider);
                    foreach (var selectorModel in matchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(centralPrefix,
                                                                                                           selectorModel.AttributeRouteModel);
                    }
                }
            }
Ejemplo n.º 28
0
 public void Apply(ApplicationModel application)
 {
     foreach (var selector in application.Controllers.Where(c => c.Filters.Any(f => f is IApiBehaviorMetadata)).SelectMany(c => c.Selectors))
     {
         if (selector.AttributeRouteModel != null)
         {
             selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_routePrefix, selector.AttributeRouteModel);
         }
         else
         {
             selector.AttributeRouteModel = _routePrefix;
         }
     }
 }
 private void ApplyControllerModel(ControllerModel controllerModel)
 {
     if (typeof(IRemoteService).IsAssignableFrom(controllerModel.ControllerType))
     {
         if (controllerModel.ApiExplorer.IsVisible == null)
         {
             controllerModel.ApiExplorer.IsVisible = true;
         }
         controllerModel.ControllerName = ApplyControllerName(controllerModel.ControllerName);
         foreach (var item in controllerModel.Selectors)
         {
             if (!controllerModel.Attributes.Any(a => a.GetType() == typeof(ApiControllerAttribute)))
             {
                 item.EndpointMetadata.Add(new ApiControllerAttribute());
             }
             if (!controllerModel.Attributes.Any(a => a.GetType() == typeof(ControllerAttribute)))
             {
                 item.EndpointMetadata.Add(new ControllerAttribute());
             }
             if (!controllerModel.Attributes.Any(a => a.GetType() == typeof(ControllerAttribute)))
             {
                 item.EndpointMetadata.Add(new RouteAttribute("[controller]"));
             }
             if (item.AttributeRouteModel == null)
             {
                 item.AttributeRouteModel = new AttributeRouteModel(new RouteAttribute("[controller]"));
             }
             if (!string.IsNullOrEmpty(_options.ServiceNamePrefix))
             {
                 var prefixRoute = new AttributeRouteModel(new RouteAttribute(_options.ServiceNamePrefix));
                 item.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(prefixRoute, item.AttributeRouteModel);
             }
         }
     }
     else if (!string.IsNullOrEmpty(_options.ControllerNamePrefix))
     {
         foreach (var item in controllerModel.Selectors)
         {
             if (item.AttributeRouteModel == null)
             {
                 item.AttributeRouteModel = new AttributeRouteModel(new RouteAttribute(_options.ControllerNamePrefix));
             }
             else
             {
                 item.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(new AttributeRouteModel(new RouteAttribute(_options.ControllerNamePrefix)), item.AttributeRouteModel);
             }
         }
     }
 }
Ejemplo n.º 30
0
    public void Combine_RightOverridesReflectedAttributeRouteModel(
        AttributeRouteModel left,
        AttributeRouteModel right)
    {
        // Arrange
        var expectedTemplate = AttributeRouteModel.CombineTemplates(null, right.Template);

        // Act
        var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right);

        // Assert
        Assert.NotNull(combined);
        Assert.Equal(expectedTemplate, combined.Template);
        Assert.Equal(combined.Order, right.Order);
    }