Esempio n. 1
0
        /// <inheritdoc />
        public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrEmpty(viewName))
            {
                throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(viewName));
            }

            if (ViewEngines.Count == 0)
            {
                throw new InvalidOperationException(Resources.FormatViewEnginesAreRequired(
                                                        typeof(MvcViewOptions).FullName,
                                                        nameof(MvcViewOptions.ViewEngines),
                                                        typeof(IViewEngine).FullName));
            }

            // Do not allocate in the common cases: ViewEngines contains one entry or initial attempt is successful.
            IEnumerable <string> searchedLocations = null;
            List <string>        searchedList      = null;

            for (var i = 0; i < ViewEngines.Count; i++)
            {
                var result = ViewEngines[i].FindView(context, viewName, isMainPage);
                if (result.Success)
                {
                    return(result);
                }

                if (searchedLocations == null)
                {
                    // First failure.
                    searchedLocations = result.SearchedLocations;
                }
                else
                {
                    if (searchedList == null)
                    {
                        // Second failure.
                        searchedList      = new List <string>(searchedLocations);
                        searchedLocations = searchedList;
                    }

                    searchedList.AddRange(result.SearchedLocations);
                }
            }

            return(ViewEngineResult.NotFound(viewName, searchedLocations ?? Enumerable.Empty <string>()));
        }
        public void FindView_IsMainPage_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegisteredWhichReturnsNotFoundResult()
        {
            // Arrange
            var viewName = "test-view";
            var engine   = new Mock <IViewEngine>(MockBehavior.Strict);

            engine
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ true))
            .Returns(ViewEngineResult.NotFound(viewName, new[] { "controller/test-view" }));
            var optionsAccessor = Options.Create(new MvcViewOptions());

            optionsAccessor.Value.ViewEngines.Add(engine.Object);
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act
            var result = compositeViewEngine.FindView(GetActionContext(), viewName, isMainPage: true);

            // Assert
            Assert.False(result.Success);
            Assert.Equal(new[] { "controller/test-view" }, result.SearchedLocations);
        }
        /// <inheritdoc />
        public ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage)
        {
            if (string.IsNullOrEmpty(viewPath))
            {
                throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(viewPath));
            }

            // Do not allocate in the common cases: ViewEngines contains one entry or initial attempt is successful.
            IEnumerable <string> searchedLocations = null;
            List <string>        searchedList      = null;

            for (var i = 0; i < ViewEngines.Count; i++)
            {
                var result = ViewEngines[i].GetView(executingFilePath, viewPath, isMainPage);
                if (result.Success)
                {
                    return(result);
                }

                if (searchedLocations == null)
                {
                    // First failure.
                    searchedLocations = result.SearchedLocations;
                }
                else
                {
                    if (searchedList == null)
                    {
                        // Second failure.
                        searchedList      = new List <string>(searchedLocations);
                        searchedLocations = searchedList;
                    }

                    searchedList.AddRange(result.SearchedLocations);
                }
            }

            return(ViewEngineResult.NotFound(viewPath, searchedLocations ?? Enumerable.Empty <string>()));
        }
        public void GetView_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegisteredWhichReturnsNotFoundResult(
            bool isMainPage)
        {
            // Arrange
            var viewName         = "test-view.cshtml";
            var expectedViewName = "~/" + viewName;
            var engine           = new Mock <IViewEngine>(MockBehavior.Strict);

            engine
            .Setup(e => e.GetView("~/Index.html", viewName, isMainPage))
            .Returns(ViewEngineResult.NotFound(expectedViewName, new[] { expectedViewName }));
            var optionsAccessor = Options.Create(new MvcViewOptions());

            optionsAccessor.Value.ViewEngines.Add(engine.Object);
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act
            var result = compositeViewEngine.GetView("~/Index.html", viewName, isMainPage);

            // Assert
            Assert.False(result.Success);
            Assert.Equal(new[] { expectedViewName }, result.SearchedLocations);
        }
        public void GetView_ReturnsViewFromFirstViewEngineWithFoundResult(bool isMainPage)
        {
            // Arrange
            var viewName         = "foo.cshtml";
            var expectedViewName = "~/" + viewName;
            var engine1          = new Mock <IViewEngine>(MockBehavior.Strict);
            var engine2          = new Mock <IViewEngine>(MockBehavior.Strict);
            var engine3          = new Mock <IViewEngine>(MockBehavior.Strict);
            var view2            = Mock.Of <IView>();
            var view3            = Mock.Of <IView>();

            engine1
            .Setup(e => e.GetView("~/Index.html", viewName, isMainPage))
            .Returns(ViewEngineResult.NotFound(expectedViewName, Enumerable.Empty <string>()));
            engine2
            .Setup(e => e.GetView("~/Index.html", viewName, isMainPage))
            .Returns(ViewEngineResult.Found(expectedViewName, view2));
            engine3
            .Setup(e => e.GetView("~/Index.html", viewName, isMainPage))
            .Returns(ViewEngineResult.Found(expectedViewName, view3));

            var optionsAccessor = Options.Create(new MvcViewOptions());

            optionsAccessor.Value.ViewEngines.Add(engine1.Object);
            optionsAccessor.Value.ViewEngines.Add(engine2.Object);
            optionsAccessor.Value.ViewEngines.Add(engine3.Object);
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act
            var result = compositeViewEngine.GetView("~/Index.html", viewName, isMainPage);

            // Assert
            Assert.True(result.Success);
            Assert.Same(view2, result.View);
            Assert.Equal(expectedViewName, result.ViewName);
        }