Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to find the view, or returns information about the locations that were searched.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="options">The view parameters.</param>
        /// <param name="view">The view.</param>
        /// <returns>
        /// A <see cref="ViewEngineResult"/> containing the resolved view or information about the locations that were searched.
        /// </returns>
        public ViewEngineResult FindView(ControllerContext controllerContext, ViewResultOptions options, string view)
        {
            if (!ShouldHandle(controllerContext, options, view))
            {
                return(new ViewEngineResult(false, new string[0]));
            }

            var possibleViewNames = NamingConvention.GetAlternativeNames(controllerContext, view);
            var allTypes          = FindTypesNamed(controllerContext, possibleViewNames);
            var candidateTypes    = FilterCandidateTypes(controllerContext, options, view, allTypes);

            var namespaces = CreateShortlistOfNamespacesInWhichToSearch(controllerContext, view);

            var match = namespaces.Select(ns => FindFirstTypeInNamespace(candidateTypes, ns)).Where(x => x != null).FirstOrDefault();

            if (match != null)
            {
                return(CreateViewResult(controllerContext, options, match));
            }

            // Make a list of all search examples used above to create a friendly error message
            var searchAttempts = namespaces.SelectMany(ns =>
                                                       possibleViewNames.Select(name => string.IsNullOrEmpty(ns)
                    ? string.Format("{0}", name)
                    : string.Format("{0}.{1}", ns, name)
                                                                                ));

            TraceSources.MagellanSource.TraceInformation("The {0} could not find the view '{1}'. The following locations were searched: \r\n{2}", GetType().FullName, view, string.Join("\r\n- ", searchAttempts.ToArray()));
            return(new ViewEngineResult(false, searchAttempts));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ChildWindowViewEngineResult"/> class.
 /// </summary>
 /// <param name="viewActivator">The view activator.</param>
 /// <param name="type">The type.</param>
 /// <param name="options">The options.</param>
 /// <param name="controllerContext">The controller context.</param>
 public ChildWindowViewEngineResult(IViewActivator viewActivator, Type type, ViewResultOptions options, ControllerContext controllerContext)
     : base(controllerContext, options)
 {
     _viewActivator = viewActivator;
     _type          = type;
     _options       = options;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ChildWindowViewEngineResult"/> class.
 /// </summary>
 /// <param name="viewActivator">The view activator.</param>
 /// <param name="type">The type.</param>
 /// <param name="options">The options.</param>
 /// <param name="controllerContext">The controller context.</param>
 public ChildWindowViewEngineResult(IViewActivator viewActivator, Type type, ViewResultOptions options, ControllerContext controllerContext)
     : base(controllerContext, options)
 {
     _viewActivator = viewActivator;
     _type = type;
     _options = options;
 }
        /// <summary>
        /// Finds the view.
        /// </summary>
        /// <param name="navigationRequest">The navigation request.</param>
        /// <param name="options">The options.</param>
        /// <param name="view">The view.</param>
        /// <returns></returns>
        public ViewEngineResult FindView(ControllerContext navigationRequest, ViewResultOptions options, string view)
        {
            if (Count == 0)
            {
                throw new NavigationConfigurationException("No view engines have been registered with the ViewEngines collection.");
            }

            var searchLocations = new List <string>();

            foreach (var viewEngine in this)
            {
                TraceSources.MagellanSource.TraceVerbose("The ViewEngineCollection is consulting the view engine '{0}' for the view '{1}'.", viewEngine.GetType().FullName, view);
                var result = viewEngine.FindView(navigationRequest, options, view);
                if (result == null)
                {
                    continue;
                }
                if (result.Success)
                {
                    return(result);
                }

                searchLocations.AddRange(result.SearchLocations);
            }

            return(new ViewEngineResult(false, searchLocations));
        }
        public void MustRegisterViewEnginesBeforeFindingViews()
        {
            var collection = new ViewEngineCollection();
            var context = RequestBuilder.CreateRequest().BuildControllerContext();
            var parameters = new ViewResultOptions();

            Assert.Throws<NavigationConfigurationException>(() => collection.FindView(context, parameters, "Foo"));
        }
Ejemplo n.º 6
0
 /// <summary>
 /// When implemented in a derived class, allows the derived class to restrict the criteria used to select potential types.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <param name="candidates">The list of candidate types.</param>
 /// <returns></returns>
 protected override IEnumerable<Type> FilterCandidateTypes(ControllerContext controllerContext, ViewResultOptions options, string viewName, IEnumerable<Type> candidates)
 {
     if (options.GetRegionName() != null || options.GetRegion() != null)
     {
         return candidates.Where(type => typeof (UIElement).IsAssignableFrom(type));
     }
     return new Type[0];
 }
        protected WindowViewEngineResult CreateResult(Type pageType, object parameters, object model)
        {
            var request = RequestBuilder.CreateRequest();

            var options = new ViewResultOptions(parameters);
            options.Add("Model", model);

            return new WindowViewEngineResult(
                pageType,
                options,
                request.BuildControllerContext(),
                new DefaultViewActivator()
                );
        }
        protected PageViewEngineResult CreateResult(Type pageType, object parameters, object model)
        {
            var request = RequestBuilder.CreateRequest("X", "Y", parameters);
            request.Navigator = Navigator;
            request.Navigator.SetupGet(x => x.Dispatcher).Returns(new SingleThreadDispatcher());
            
            var options = new ViewResultOptions(parameters);
            options.SetModel(model);

            return new PageViewEngineResult(
                pageType,
                options, 
                request.BuildControllerContext(),
                new DefaultViewActivator()
                );
        }
        protected WindowViewEngineResult CreateResult(object page, object parameters, object model)
        {
            var request = RequestBuilder.CreateRequest();
            
            var options = new ViewResultOptions(parameters);
            options.Add("Model", model);

            var viewActivator = new Mock<IViewActivator>();
            viewActivator.Setup(x => x.Instantiate(It.IsAny<Type>())).Returns(page);

            return new WindowViewEngineResult(
                page.GetType(),
                options,
                request.BuildControllerContext(),
                viewActivator.Object
                );
        }
        protected PageViewEngineResult CreateResult(object page, object parameters, object model)
        {
            var request = RequestBuilder.CreateRequest("X", "Y", parameters);
            request.Navigator = Navigator;
            request.Navigator.SetupGet(x => x.Dispatcher).Returns(new SingleThreadDispatcher());

            var options = new ViewResultOptions(parameters);
            options.SetModel(model);

            var viewActivator = new Mock<IViewActivator>();
            viewActivator.Setup(x => x.Instantiate(It.IsAny<Type>())).Returns(page);

            return new PageViewEngineResult(
                page.GetType(),
                options,
                request.BuildControllerContext(),
                viewActivator.Object
                );
        }
Ejemplo n.º 11
0
 /// <summary>
 /// When implemented in a derived class, allows the derived class to restrict the criteria used to select potential types.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <param name="candidates">The list of candidate types.</param>
 /// <returns></returns>
 protected override IEnumerable<Type> FilterCandidateTypes(ControllerContext controllerContext, ViewResultOptions options, string viewName, IEnumerable<Type> candidates)
 {
     return candidates.Where(c => typeof(UserControl).IsAssignableFrom(c));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// When implemented in a derived class, indicated whether this view engine should even attempt to
 /// locate views and handle the request. If this method returns false, no reflection will be
 /// performed.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <returns></returns>
 protected override bool ShouldHandle(ControllerContext controllerContext, ViewResultOptions options, string viewName)
 {
     return options.GetViewType() == "Page"
            && base.ShouldHandle(controllerContext, options, viewName);
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PageViewEngineResult"/> class.
 /// </summary>
 /// <param name="viewActivator">The view activator.</param>
 /// <param name="type">The type.</param>
 /// <param name="options">The options.</param>
 /// <param name="controllerContext">The controller context.</param>
 public PageViewEngineResult(IViewActivator viewActivator, Type type, ViewResultOptions options, ControllerContext controllerContext) 
     : base(controllerContext, options)
 {
     _viewActivator = viewActivator;
     _type = type;
 }
Ejemplo n.º 14
0
 /// <summary>
 /// When implemented in a derived class, allows the derived class to restrict the criteria used to select potential types.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <param name="candidates">The list of candidate types.</param>
 /// <returns></returns>
 protected override IEnumerable<Type> FilterCandidateTypes(ControllerContext controllerContext, ViewResultOptions options, string viewName, IEnumerable<Type> candidates)
 {
     return candidates
         .Where(type => typeof(Page).IsAssignableFrom(type)
             || (typeof(ContentControl).IsAssignableFrom(type) && !typeof(Window).IsAssignableFrom(type)));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="WindowViewEngineResult"/> class.
 /// </summary>
 /// <param name="viewType">Type of the view.</param>
 /// <param name="options">The options.</param>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="viewActivator">The view activator.</param>
 public WindowViewEngineResult(Type viewType, ViewResultOptions options, ControllerContext controllerContext, IViewActivator viewActivator)
     : base(controllerContext, options)
 {
     this.viewType      = viewType;
     this.viewActivator = viewActivator;
 }
Ejemplo n.º 16
0
 /// <summary>
 /// When implemented in a derived class, allows the derived class to restrict the criteria used to select potential types.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <param name="candidates">The list of candidate types.</param>
 /// <returns></returns>
 protected override IEnumerable <Type> FilterCandidateTypes(ControllerContext controllerContext, ViewResultOptions options, string viewName, IEnumerable <Type> candidates)
 {
     return(candidates.Where(type => typeof(Window).IsAssignableFrom(type)));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PageViewEngineResult"/> class.
 /// </summary>
 /// <param name="viewType">Type of the view.</param>
 /// <param name="options">The options.</param>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="viewActivator">The view activator.</param>
 public PageViewEngineResult(Type viewType, ViewResultOptions options, ControllerContext controllerContext, IViewActivator viewActivator)
     : base(controllerContext, options)
 {
     this.viewType = viewType;
     this.viewActivator = viewActivator;
 }
 /// <summary>
 /// When implemented in a derived class, allows the derived class to restrict the criteria used to select potential types.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <param name="candidates">The list of candidate types.</param>
 /// <returns></returns>
 protected abstract IEnumerable<Type> FilterCandidateTypes(ControllerContext controllerContext, ViewResultOptions options, string viewName, IEnumerable<Type> candidates);
Ejemplo n.º 19
0
 /// <summary>
 /// When implemented in a derived class, indicated whether this view engine should even attempt to locate views and handle the request.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <returns></returns>
 protected override bool ShouldHandle(ControllerContext controllerContext, ViewResultOptions options, string viewName)
 {
     var viewType = options.GetViewType();
     return viewType == "Dialog" || viewType == "Window";
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Creates the view result for the specified view type.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 protected override ViewEngineResult CreateViewResult(ControllerContext controllerContext, ViewResultOptions options, Type type)
 {
     return new WindowViewEngineResult(type, options, controllerContext, viewActivator);
 }
Ejemplo n.º 21
0
 /// <summary>
 /// When implemented in a derived class, indicated whether this view engine should even attempt to locate views and handle the request.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <returns></returns>
 protected override bool ShouldHandle(ControllerContext controllerContext, ViewResultOptions options, string viewName)
 {
     return(options.GetViewType() == "Page");
 }
Ejemplo n.º 22
0
 /// <summary>
 /// When implemented in a derived class, indicated whether this view engine should even attempt to locate views and handle the request.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <returns></returns>
 protected override bool ShouldHandle(ControllerContext controllerContext, ViewResultOptions options, string viewName)
 {
     return options.GetViewType() == "CompositeView";
 }
Ejemplo n.º 23
0
 /// <summary>
 /// When implemented in a derived class, indicated whether this view engine should even attempt to
 /// locate views and handle the request. If this method returns false, no reflection will be
 /// performed.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <returns></returns>
 protected override bool ShouldHandle(ControllerContext controllerContext, ViewResultOptions options, string viewName)
 {
     return(options.GetViewType() == "ChildWindow" &&
            base.ShouldHandle(controllerContext, options, viewName));
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Creates the view result for the specified view type.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 protected override ViewEngineResult CreateViewResult(ControllerContext controllerContext, ViewResultOptions options, Type type)
 {
     return new PageViewEngineResult(_viewActivator, type, options, controllerContext);
 }
        /// <summary>
        /// Attempts to find the view, or returns information about the locations that were searched.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="options">The view parameters.</param>
        /// <param name="view">The view.</param>
        /// <returns>
        /// A <see cref="ViewEngineResult"/> containing the resolved view or information about the locations that were searched.
        /// </returns>
        public ViewEngineResult FindView(ControllerContext controllerContext, ViewResultOptions options, string view)
        {
            if (!ShouldHandle(controllerContext, options, view))
            {
                return new ViewEngineResult(false, new string[0]);
            }

            var possibleViewNames = NamingConvention.GetAlternativeNames(controllerContext, view);
            var allTypes = FindTypesNamed(controllerContext, possibleViewNames);
            var candidateTypes = FilterCandidateTypes(controllerContext, options, view, allTypes);

            var namespaces = CreateShortlistOfNamespacesInWhichToSearch(controllerContext, view);

            var match = namespaces.Select(ns => FindFirstTypeInNamespace(candidateTypes, ns)).Where(x => x != null).FirstOrDefault();
            if (match != null)
            {
                return CreateViewResult(controllerContext, options, match);
            }

            // Make a list of all search examples used above to create a friendly error message
            var searchAttempts = namespaces.SelectMany(ns => 
                possibleViewNames.Select(name => string.IsNullOrEmpty(ns) 
                    ? string.Format("{0}", name) 
                    : string.Format("{0}.{1}", ns, name)
                    ));

            TraceSources.MagellanSource.TraceInformation("The {0} could not find the view '{1}'. The following locations were searched: \r\n{2}", GetType().FullName, view, string.Join("\r\n- ", searchAttempts.ToArray()));
            return new ViewEngineResult(false, searchAttempts);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CompositeViewEngineResult"/> class.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The options.</param>
 /// <param name="type">The type.</param>
 /// <param name="viewActivator">The view activator.</param>
 public CompositeViewEngineResult(ControllerContext controllerContext, ViewResultOptions options, Type type, IViewActivator viewActivator)
     : base(controllerContext, options)
 {
     this.type = type;
     this.viewActivator = viewActivator;
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PageViewEngineResult"/> class.
 /// </summary>
 /// <param name="viewActivator">The view activator.</param>
 /// <param name="type">The type.</param>
 /// <param name="options">The options.</param>
 /// <param name="controllerContext">The controller context.</param>
 public PageViewEngineResult(IViewActivator viewActivator, Type type, ViewResultOptions options, ControllerContext controllerContext)
     : base(controllerContext, options)
 {
     _viewActivator = viewActivator;
     _type          = type;
 }
Ejemplo n.º 28
0
        /// <summary>
        /// When implemented in a derived class, indicated whether this view engine should even attempt to locate views and handle the request.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="options">The view parameters.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns></returns>
        protected override bool ShouldHandle(ControllerContext controllerContext, ViewResultOptions options, string viewName)
        {
            var viewType = options.GetViewType();

            return(viewType == "Dialog" || viewType == "Window");
        }
Ejemplo n.º 29
0
 /// <summary>
 /// When implemented in a derived class, allows the derived class to restrict the criteria used to select potential types.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <param name="candidates">The list of candidate types.</param>
 /// <returns></returns>
 protected abstract IEnumerable <Type> FilterCandidateTypes(ControllerContext controllerContext, ViewResultOptions options, string viewName, IEnumerable <Type> candidates);
Ejemplo n.º 30
0
 /// <summary>
 /// Creates the view result for the specified view type.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 protected override ViewEngineResult CreateViewResult(ControllerContext controllerContext, ViewResultOptions options, Type type)
 {
     return(new WindowViewEngineResult(type, options, controllerContext, viewActivator));
 }
Ejemplo n.º 31
0
 /// <summary>
 /// Creates the view result for the specified view type.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 protected abstract ViewEngineResult CreateViewResult(ControllerContext controllerContext, ViewResultOptions options, Type type);
 /// <summary>
 /// When implemented in a derived class, indicated whether this view engine should even attempt to 
 /// locate views and handle the request. If this method returns false, no reflection will be 
 /// performed.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <returns></returns>
 protected virtual bool ShouldHandle(ControllerContext controllerContext, ViewResultOptions options, string viewName)
 {
     return true;
 }
Ejemplo n.º 33
0
 /// <summary>
 /// Creates the view result for the specified view type.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 protected override ViewEngineResult CreateViewResult(ControllerContext controllerContext, ViewResultOptions options, Type type)
 {
     return new CompositeViewEngineResult(controllerContext, options, type, viewActivator);
 }
 /// <summary>
 /// Creates the view result for the specified view type.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 protected abstract ViewEngineResult CreateViewResult(ControllerContext controllerContext, ViewResultOptions options, Type type);
 protected override IEnumerable<Type> FilterCandidateTypes(ControllerContext controllerContext, ViewResultOptions viewParameters, string viewName, IEnumerable<Type> candidates)
 {
     return candidates;
 }
Ejemplo n.º 36
0
 /// <summary>
 /// When implemented in a derived class, indicated whether this view engine should even attempt to
 /// locate views and handle the request. If this method returns false, no reflection will be
 /// performed.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="options">The view parameters.</param>
 /// <param name="viewName">Name of the view.</param>
 /// <returns></returns>
 protected virtual bool ShouldHandle(ControllerContext controllerContext, ViewResultOptions options, string viewName)
 {
     return(true);
 }
 protected override ViewEngineResult CreateViewResult(ControllerContext controllerContext, ViewResultOptions viewParameters, Type type)
 {
     return new ViewEngineResult(true, null);
 }