public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            bindingContext.ThrowIfNull(nameof(bindingContext));

            IViewModel          model = null;
            ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            //value is n
            if (!value.Any())
            {
                return(null);
            }

            var passedModel = value;

            if (passedModel != null && bindingContext.ModelType.IsAssignableFrom(passedModel.GetType()))
            {
                //route already available and of the correct type so return it.
                return(null);
            }

            string modelData = string.IsNullOrEmpty(passedModel.FirstValue) ? "/" : passedModel.FirstValue;

            //return null if modeldata is not set.
            if (modelData == null)
            {
                return(null);
            }

            //generate a Cache key for the current model.
            //try to get model out of HttpContext.Items[], incase the same model is request multiple times during
            //a single request.
            string key = MODEL_KEY_FORMAT.FormatString(modelData);

            //model = bindingContext.HttpContext.Items[key] as IViewModel;
            if (model == null)
            {
                model = modelData.StartsWith("tcm:") ?
                        _contentProvider.BuildViewModel(new TcmUri(modelData)) :
                        _contentProvider.BuildViewModel(modelData);

                if (model != null && !bindingContext.ModelType.IsAssignableFrom(model.GetType()))
                {
                    //Model is incorrect, so remove
                    model = null;
                }

                bindingContext.HttpContext.Items[key] = model;
            }
            //if (model is IRenderable)
            //    controllerContext.RouteData.DataTokens["area"] = ((IRenderable)model).MvcData.Area;

            //Todo: WTF... this doesn't make sense; Bug in Microsoft.AspNetCore.Mvc???
            bindingContext.Result = ModelBindingResult.Success(model);
            //Todo: implemenet async
            return(Task.FromResult(ModelBindingResult.Success(model)));
        }
示例#2
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            bindingContext.ThrowIfNull(nameof(bindingContext));

            ISitemapItem model = null;
            //navType & navLevels are set on the 'RouteData' Metadata field on the component template.
            //and passed as routeparameters to the action.
            string navType       = null;
            int    navLevels     = 0;
            int    navStartLevel = -1;

            ValueProviderResult value               = bindingContext.ValueProvider.GetValue("navType");
            ValueProviderResult navLevelsResult     = bindingContext.ValueProvider.GetValue("navLevels");
            ValueProviderResult navStartLevelResult = bindingContext.ValueProvider.GetValue("navStartLevel");

            if (value.Any())
            {
                navType = value.FirstValue;
            }

            string requestUrlPath = bindingContext.HttpContext.Request.Path.Value;

            ////Filename should be removed. the data published from Tridion doesnt include the filename into the url's
            ////i.e. for url '/product/index.html' published content from Tridion contains: '/products/'
            //var fileName = Path.GetFileName(requestUrlPath);
            //if (!fileName.IsNullOrEmpty())
            //    requestUrlPath = requestUrlPath.Replace(fileName, string.Empty);

            if (navLevelsResult.Any())
            {
                int.TryParse(navLevelsResult.FirstValue, out navLevels);
            }
            if (navStartLevelResult.Any())
            {
                int.TryParse(navStartLevelResult.FirstValue, out navStartLevel);
            }

            //generate a Cache key for the current model.
            //try to get model out of HttpContext.Items[], incase the same model is request multiple times during
            //a single request.
            string key = MODEL_KEY_FORMAT.FormatString(navType, requestUrlPath, navLevels, navStartLevel);

            model = bindingContext.HttpContext.Items[key] as ISitemapItem;

            if (model == null)
            {
                switch (navType?.ToLower())
                {
                case "children":
                    model = _navigationProvider.GetChildren(requestUrlPath, navLevels, navStartLevel);
                    break;

                case "path":
                    model = _navigationProvider.GetPath(requestUrlPath);
                    break;

                default:
                    model = _navigationProvider.GetAll(navLevels);
                    break;
                }
                bindingContext.HttpContext.Items[key] = model;
            }
            //Todo: WTF... this doesn't make sense; Bug in Microsoft.AspNetCore.Mvc???
            bindingContext.Result = ModelBindingResult.Success(model);
            //Todo: implemenet async
            return(Task.FromResult(ModelBindingResult.Success(model)));
        }