⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlsitemapprovider.cs

📁 MasterPage(母版页) 母版页(MasterPage)就相当于模板页
💻 CS
📖 第 1 页 / 共 3 页
字号:
            string attrName, ref string text, bool allowImplicitResource) {
            if (String.IsNullOrEmpty(text)) {
                return;
            }

            string resourceKey = null;
            string temp = text.TrimStart(new char[] { ' ' });

            if (temp != null && temp.Length > _resourcePrefixLength) {
                if (temp.ToLower(CultureInfo.InvariantCulture).StartsWith(_resourcePrefix, StringComparison.Ordinal)) {
                    if (!allowImplicitResource) {
                        throw new ConfigurationErrorsException(
                            SR.GetString(SR.XmlSiteMapProvider_multiple_resource_definition, attrName), xmlNode);
                    }

                    resourceKey = temp.Substring(_resourcePrefixLength + 1);

                    if (resourceKey.Length == 0) {
                        throw new ConfigurationErrorsException(
                            SR.GetString(SR.XmlSiteMapProvider_resourceKey_cannot_be_empty), xmlNode);
                    }

                    // Retrieve className from attribute
                    string className = null;
                    string key = null;
                    int index = resourceKey.IndexOf(_resourceKeySeparator);
                    if (index == -1) {
                        throw new ConfigurationErrorsException(
                            SR.GetString(
                            SR.XmlSiteMapProvider_invalid_resource_key, resourceKey), xmlNode);
                    }

                    className = resourceKey.Substring(0, index);
                    key = resourceKey.Substring(index + 1);

                    // Retrieve resource key and default value from attribute
                    int defaultIndex = key.IndexOf(_resourceKeySeparator);
                    if (defaultIndex != -1) {
                        text = key.Substring(defaultIndex + 1);
                        key = key.Substring(0, defaultIndex);
                    }
                    else {
                        text = null;
                    }

                    if (collection == null) {
                        collection = new NameValueCollection();
                    }

                    collection.Add(attrName, className.Trim());
                    collection.Add(attrName, key.Trim());
                }
            }
        }

        private SiteMapNode GetNodeFromXmlNode(XmlNode xmlNode, Queue queue) {
            SiteMapNode node = null;
            // static nodes
            string title = null, url = null, description = null, roles = null, resourceKey = null;

            // Url attribute is NOT required for a xml node.
            SecUtility.GetAndRemoveStringAttribute(xmlNode, "url", ref url);
            SecUtility.GetAndRemoveStringAttribute(xmlNode, "title", ref title);
            SecUtility.GetAndRemoveStringAttribute(xmlNode, "description", ref description);
            SecUtility.GetAndRemoveStringAttribute(xmlNode, "roles", ref roles);
            SecUtility.GetAndRemoveStringAttribute(xmlNode, "resourceKey", ref resourceKey);

            // Do not add the resourceKey if the resource is not valid.
            if (!String.IsNullOrEmpty(resourceKey) && 
                !ValidateResource(ResourceKey, resourceKey + ".title")) {
                resourceKey = null;
            }

            SecUtility.CheckForbiddenAttribute(xmlNode, _securityTrimmingEnabledAttrName);

            NameValueCollection resourceKeyCollection = null;
            bool allowImplicitResourceAttribute = String.IsNullOrEmpty(resourceKey);
            HandleResourceAttribute(xmlNode, ref resourceKeyCollection, 
                "title", ref title, allowImplicitResourceAttribute);
            HandleResourceAttribute(xmlNode, ref resourceKeyCollection, 
                "description", ref description, allowImplicitResourceAttribute);

            ArrayList roleList = new ArrayList();
            if (roles != null) {
                int foundIndex = roles.IndexOf('?');
                if (foundIndex != -1) {
                    throw new ConfigurationErrorsException(
                        SR.GetString(SR.Auth_rule_names_cant_contain_char,
                            roles[foundIndex].ToString(CultureInfo.InvariantCulture)), xmlNode);
                }

                foreach (string role in roles.Split(_seperators)) {
                    string trimmedRole = role.Trim();
                    if (trimmedRole.Length > 0) {
                        roleList.Add(trimmedRole);
                    }
                }
            }
            roleList = ArrayList.ReadOnly(roleList);

            String key = null;

            // Make urls absolute.
            if (!String.IsNullOrEmpty(url)) {
                // URL needs to be trimmed.
                url = url.Trim();

                if (!SecUtility.IsAbsolutePhysicalPath(url)) {
                    if (SecUtility.IsRelativeUrl(url)) {
                        string virtualPath = url;

                        int qs = url.IndexOf('?');
                        if (qs != -1) {
                            virtualPath = url.Substring(0, qs);
                        }

                        // Make sure the path is adjusted properly
                        virtualPath = 
                            VirtualPathUtility.Combine(AppDomainAppVirtualPathWithTrailingSlash, virtualPath);

                        // Make it an absolute virtualPath
                        virtualPath = VirtualPathUtility.ToAbsolute(virtualPath);

                        if (qs != -1) {
                            virtualPath += url.Substring(qs);
                        }

                        url = virtualPath;
                    }
                }

                // Reject any suspicious or mal-formed Urls.
                string decodedUrl = HttpUtility.UrlDecode(url);
                if (!String.Equals(url, decodedUrl, StringComparison.Ordinal)) {
                    throw new ConfigurationErrorsException(
                        SR.GetString(SR.Property_Had_Malformed_Url, "url", url), xmlNode);
                }

                key = url.ToLowerInvariant();
            }
            else {
                key = Guid.NewGuid().ToString();
            }

            // attribute collection does not contain pre-defined properties like title, url, etc.
            ReadOnlyNameValueCollection attributeCollection = new ReadOnlyNameValueCollection();
            attributeCollection.SetReadOnly(false);
            foreach (XmlAttribute attribute in xmlNode.Attributes) {
                string value = attribute.Value;
                HandleResourceAttribute(xmlNode, ref resourceKeyCollection, attribute.Name, ref value, allowImplicitResourceAttribute);
                attributeCollection[attribute.Name] = value;
            }
            attributeCollection.SetReadOnly(true);

            node = new SiteMapNode(this, key, url, title, description, roleList, attributeCollection, resourceKeyCollection, resourceKey);
            node.ReadOnly = true;

            foreach (XmlNode subNode in xmlNode.ChildNodes) {
                if (subNode.NodeType != XmlNodeType.Element)
                    continue;

                queue.Enqueue(node);
                queue.Enqueue(subNode);
            }

            return node;
        }

        private SiteMapProvider GetProviderFromName(string providerName) {
            Debug.Assert(providerName != null);

            SiteMapProvider provider = SiteMap.Providers[providerName];
            if (provider == null) {
                throw new ProviderException(SR.GetString(SR.Provider_Not_Found, providerName));
            }

            return provider;
        }

        protected override SiteMapNode GetRootNodeCore() {
            BuildSiteMap();
            return _siteMapNode;
        }


        public override void Initialize(string name, NameValueCollection attributes) {
            if (_initialized) {
                throw new InvalidOperationException(
                    SR.GetString(SR.XmlSiteMapProvider_Cannot_Be_Inited_Twice));
            }

            if (attributes != null) {
                if (string.IsNullOrEmpty(attributes["description"])) {
                    attributes.Remove("description");
                    attributes.Add("description", SR.GetString(SR.XmlSiteMapProvider_Description));
                }

                string siteMapFile = null;
                SecUtility.GetAndRemoveStringAttribute(attributes, _siteMapFileAttribute, name, ref siteMapFile);
                _virtualPath = siteMapFile;
            }

            base.Initialize(name, attributes);

            if (attributes != null) {
                SecUtility.CheckUnrecognizedAttributes(attributes, name);
            }

            _initialized = true;
        }

        private void Initialize(string virtualPath, bool secuityTrimmingEnabled) {
            NameValueCollection coll = new NameValueCollection();
            coll.Add(_siteMapFileAttribute, virtualPath);
            coll.Add(_securityTrimmingEnabledAttrName, SecUtility.GetStringFromBool(secuityTrimmingEnabled));

            // Use the siteMapFile virtual path as the provider name
            Initialize(virtualPath, coll);
        }

        //private void OnConfigFileChange(Object sender, FileChangeEvent e)
        //{
        //    // Notifiy the parent for the change.
        //    XmlSiteMapProvider parentProvider = ParentProvider as XmlSiteMapProvider;
        //    if (parentProvider != null)
        //    {
        //        parentProvider.OnConfigFileChange(sender, e);
        //    }
        //    Clear();
        //}

        protected override void RemoveNode(SiteMapNode node) {
            if (node == null) {
                throw new ArgumentNullException("node");
            }

            SiteMapProvider ownerProvider = node.Provider;

            if (ownerProvider != this) {

                // Only nodes defined in this provider tree can be removed.
                SiteMapProvider parentProvider = ownerProvider.ParentProvider;
                while (parentProvider != this) {
                    if (parentProvider == null) {
                        // Cannot remove nodes defined in other providers
                        throw new InvalidOperationException(SR.GetString(
                            SR.XmlSiteMapProvider_cannot_remove_node, node.ToString(), 
                            this.Name, ownerProvider.Name));
                    }

                    parentProvider = parentProvider.ParentProvider;
                }
            }

            if (node.Equals(((XmlSiteMapProvider)ownerProvider).GetRootNodeCore())) {
                throw new InvalidOperationException(SR.GetString(SR.SiteMapProvider_cannot_remove_root_node));
            }

            if (ownerProvider != this) {
                // Remove node from the owner provider.
                ((XmlSiteMapProvider)ownerProvider).RemoveNode(node);
            }

            base.RemoveNode(node);
        }

        protected virtual void RemoveProvider(string providerName) {
            if (providerName == null) {
                throw new ArgumentNullException("providerName");
            }

            lock (_lock) {
                SiteMapProvider provider = GetProviderFromName(providerName);
                SiteMapNode rootNode = (SiteMapNode)ChildProviderTable[provider];

                if (rootNode == null) {
                    throw new InvalidOperationException(SR.GetString(SR.XmlSiteMapProvider_cannot_find_provider, provider.Name, this.Name));
                }

                provider.ParentProvider = null;
                ChildProviderTable.Remove(provider);
                _childProviderList = null;

                base.RemoveNode(rootNode);
            }
        }

        // Note that this only returns false if the classKey cannot be found, regardless of resourceKey.
        private bool ValidateResource(string classKey, string resourceKey) {
            try {
                HttpContext.GetGlobalResourceObject(classKey, resourceKey);
            }
            catch (MissingManifestResourceException) {
                return false;
            }

            return true;
        }

        private class ReadOnlyNameValueCollection : NameValueCollection {

            public ReadOnlyNameValueCollection() {
                IsReadOnly = true;
            }

            internal void SetReadOnly(bool isReadonly) {
                IsReadOnly = isReadonly;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -