Ejemplo n.º 1
0
        internal DisplayInfo GetDisplayInfoForVirtualPath(
            string virtualPath,
            HttpContextBase httpContext,
            Func <string, bool> virtualPathExists,
            IDisplayMode currentDisplayMode,
            bool requireConsistentDisplayMode
            )
        {
            // Performance sensitive
            int first = FindFirstAvailableDisplayMode(
                currentDisplayMode,
                requireConsistentDisplayMode
                );

            for (int i = first; i < _displayModes.Count; i++)
            {
                IDisplayMode mode = _displayModes[i];
                if (mode.CanHandleContext(httpContext))
                {
                    DisplayInfo info = mode.GetDisplayInfo(
                        httpContext,
                        virtualPath,
                        virtualPathExists
                        );
                    if (info != null)
                    {
                        return(info);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to create a WebPageBase instance from a virtualPath and wraps complex compiler exceptions with simpler messages
        /// </summary>
        protected virtual WebPageBase CreatePageFromVirtualPath(
            string virtualPath,
            HttpContextBase httpContext,
            Func <string, bool> virtualPathExists,
            DisplayModeProvider displayModeProvider,
            IDisplayMode displayMode
            )
        {
            try
            {
                DisplayInfo resolvedDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(
                    virtualPath,
                    httpContext,
                    virtualPathExists,
                    displayMode
                    );

                if (resolvedDisplayInfo != null)
                {
                    var webPage = VirtualPathFactory.CreateInstance <WebPageBase>(
                        resolvedDisplayInfo.FilePath
                        );

                    if (webPage != null)
                    {
                        // Give it its virtual path
                        webPage.VirtualPath         = virtualPath;
                        webPage.VirtualPathFactory  = VirtualPathFactory;
                        webPage.DisplayModeProvider = DisplayModeProvider;

                        return(webPage);
                    }
                }
            }
            catch (HttpException e)
            {
                // If the path uses an unregistered extension, such as Foo.txt,
                // then an error regarding build providers will be thrown.
                // Check if this is the case and throw a simpler error.
                BuildManagerExceptionUtil.ThrowIfUnsupportedExtension(virtualPath, e);

                // If the path uses an extension registered with codedom, such as Foo.js,
                // then an unfriendly compilation error might get thrown by the underlying compiler.
                // Check if this is the case and throw a simpler error.
                BuildManagerExceptionUtil.ThrowIfCodeDomDefinedExtension(virtualPath, e);

                // Rethrow any errors
                throw;
            }
            // The page is missing, could not be compiled or is of an invalid type.
            throw new HttpException(
                      String.Format(
                          CultureInfo.CurrentCulture,
                          WebPageResources.WebPage_InvalidPageType,
                          virtualPath
                          )
                      );
        }
Ejemplo n.º 3
0
        private static string GetRouteLevelMatch(string pathValue, string[] supportedExtensions, Func <string, bool> virtualPathExists, HttpContextBase context, DisplayModeProvider displayModeProvider)
        {
            for (int i = 0; i < supportedExtensions.Length; i++)
            {
                string supportedExtension = supportedExtensions[i];

                // For performance, avoid multiple calls to String.Concat
                string virtualPath;
                // Only add the extension if it's not already there
                if (!PathHelpers.EndsWithExtension(pathValue, supportedExtension))
                {
                    virtualPath = "~/" + pathValue + "." + supportedExtension;
                }
                else
                {
                    virtualPath = "~/" + pathValue;
                }
                DisplayInfo virtualPathDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, context, virtualPathExists, currentDisplayMode: null);

                if (virtualPathDisplayInfo != null)
                {
                    // If there's an exact match on disk, return it unless it starts with an underscore
                    if (Path.GetFileName(virtualPathDisplayInfo.FilePath).StartsWith("_", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new HttpException(404, WebPageResources.WebPageRoute_UnderscoreBlocked);
                    }

                    string resolvedVirtualPath = virtualPathDisplayInfo.FilePath;

                    // Matches are not expected to be virtual paths so remove the ~/ from the match
                    if (resolvedVirtualPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
                    {
                        resolvedVirtualPath = resolvedVirtualPath.Remove(0, 2);
                    }

                    DisplayModeProvider.SetDisplayMode(context, virtualPathDisplayInfo.DisplayMode);

                    return(resolvedVirtualPath);
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        private static string GetRouteLevelMatch(string pathValue, IEnumerable <string> supportedExtensions, IVirtualPathFactory virtualPathFactory, HttpContextBase context, DisplayModeProvider displayModeProvider)
        {
            foreach (string supportedExtension in supportedExtensions)
            {
                string virtualPath = "~/" + pathValue;

                // Only add the extension if it's not already there
                if (!virtualPath.EndsWith("." + supportedExtension, StringComparison.OrdinalIgnoreCase))
                {
                    virtualPath += "." + supportedExtension;
                }
                DisplayInfo virtualPathDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, context, virtualPathFactory.Exists, currentDisplayMode: null);

                if (virtualPathDisplayInfo != null)
                {
                    // If there's an exact match on disk, return it unless it starts with an underscore
                    if (Path.GetFileName(virtualPathDisplayInfo.FilePath).StartsWith("_", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new HttpException(404, WebPageResources.WebPageRoute_UnderscoreBlocked);
                    }

                    string resolvedVirtualPath = virtualPathDisplayInfo.FilePath;

                    // Matches are not expected to be virtual paths so remove the ~/ from the match
                    if (resolvedVirtualPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
                    {
                        resolvedVirtualPath = resolvedVirtualPath.Remove(0, 2);
                    }

                    DisplayModeProvider.SetDisplayMode(context, virtualPathDisplayInfo.DisplayMode);

                    return(resolvedVirtualPath);
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
		private StateDisplayInfo GetStateDisplayInfo(string displayModes, HttpContextBase context)
		{
			StateDisplayInfo stateDisplayInfo = new StateDisplayInfo();
			stateDisplayInfo.IsPostBack = true;
			stateDisplayInfo.Page = State.Page;
			stateDisplayInfo.DisplayModes = displayModes;
			stateDisplayInfo.Route = State.Route;
			stateDisplayInfo.RouteName = State.GetRouteName(false);
			stateDisplayInfo.Masters = State.Masters;
			stateDisplayInfo.Theme = State.Theme;
			string[] displayModeIds = Regex.Split(displayModes, "\\|");
			DisplayInfo displayInfo = new DisplayInfo(State.Page, DisplayModeProvider.Instance.Modes.Where(m => m.DisplayModeId == displayModeIds[0]).First());
			stateDisplayInfo.Page = GetPageForDisplayInfo(displayInfo, context);
			if (StringComparer.OrdinalIgnoreCase.Compare(State.Page, stateDisplayInfo.Page) != 0)
			{
				stateDisplayInfo.Masters = new ReadOnlyCollection<string>(new string[0]);
				stateDisplayInfo.Theme = string.Empty;
			}
			return stateDisplayInfo;
		}
Ejemplo n.º 6
0
		/// <summary>
		/// Gets the browser specific theme from the given <paramref name="displayInfo"/> and is called
		/// when the page posts back
		/// </summary>
		/// <param name="displayInfo">Contains the theme for the browser specific page and the browser
		/// specific <see cref="System.Web.WebPages.IDisplayMode"/></param>
		/// <param name="context">The current <see cref="System.Web.HttpContextBase"/></param>
		/// <returns>The browser specific theme</returns>
		protected virtual string GetThemeForDisplayInfo(DisplayInfo displayInfo, HttpContextBase context)
		{
			if (displayInfo.DisplayMode.DisplayModeId.Length != 0)
				return displayInfo.FilePath + "." + displayInfo.DisplayMode.DisplayModeId;
			return displayInfo.FilePath;
		}
Ejemplo n.º 7
0
		/// <summary>
		/// Gets the browser specific master from the given <paramref name="displayInfo"/> and is called
		/// when the page posts back
		/// </summary>
		/// <param name="displayInfo">Contains the master for the browser specific page or master and the
		/// browser specific <see cref="System.Web.WebPages.IDisplayMode"/></param>
		/// <param name="context">The current <see cref="System.Web.HttpContextBase"/></param>
		/// <returns>The browser specific master</returns>
		protected virtual string GetMasterForDisplayInfo(DisplayInfo displayInfo, HttpContextBase context)
		{
			if (displayInfo.DisplayMode.DisplayModeId.Length != 0)
				return displayInfo.FilePath.Substring(0, displayInfo.FilePath.Length - 6) + displayInfo.DisplayMode.DisplayModeId + ".Master";
			return displayInfo.FilePath;
		}
Ejemplo n.º 8
0
		private void UpdateStateDisplayInfo(StateDisplayInfo stateDisplayInfo, Page page, HttpContextBase context)
		{
			string[] displayModeIds = Regex.Split(stateDisplayInfo.DisplayModes, "\\|");
			int i = 1;
			MasterPage master = null;
			bool masterSwitch = false;
			DisplayInfo displayInfo;
			if (stateDisplayInfo.Masters.Count != 0 && displayModeIds.Length > i)
			{
				displayInfo = new DisplayInfo(stateDisplayInfo.Masters[0], DisplayModeProvider.Instance.Modes.Where(m => m.DisplayModeId == displayModeIds[i]).First());
				page.MasterPageFile = GetMasterForDisplayInfo(displayInfo, context);
				page.Master.ID = "m";
				master = page.Master;
				if (stateDisplayInfo.Masters.Count > 1 && displayModeIds[i].Length == 0)
					master.MasterPageFile = stateDisplayInfo.Masters[1];
				else
					masterSwitch = true;
				i++;
			}
			while (master != null && !string.IsNullOrEmpty(master.MasterPageFile) && displayModeIds.Length > i)
			{
				displayInfo = new DisplayInfo(master.MasterPageFile, DisplayModeProvider.Instance.Modes.Where(m => m.DisplayModeId == displayModeIds[i]).First());
				master.MasterPageFile = GetMasterForDisplayInfo(displayInfo, context);
				master.Master.ID = "m";
				master = master.Master;
				if (!masterSwitch && stateDisplayInfo.Masters.Count > i && displayModeIds[i].Length == 0)
					master.MasterPageFile = stateDisplayInfo.Masters[i];
				else
					masterSwitch = true;
				i++;
			}
			if (stateDisplayInfo.Theme.Length != 0 && displayModeIds.Length > i)
			{
				displayInfo = new DisplayInfo(stateDisplayInfo.Theme, DisplayModeProvider.Instance.Modes.Where(m => m.DisplayModeId == displayModeIds[i]).First());
				page.Theme = GetThemeForDisplayInfo(displayInfo, context);
			}
		}
		private string GetDisplayModeId(DisplayInfo displayInfo)
		{
			var localizedDisplayInfo = displayInfo as LocalizedDisplayInfo;
			if (localizedDisplayInfo != null)
			{
				return localizedDisplayInfo.DisplayModeId;
			}

			return displayInfo.DisplayMode.DisplayModeId;
		}