Ejemplo n.º 1
0
        /// <summary>
        /// Refreshes the internally-cached collection of URL templates and redirection mappings.
        /// </summary>
        protected void RefreshUrlMappingData()
        {
            if (_coll != null)
            {
                _coll.Clear();
            }
            else
            {
                _coll = new UrlMappingItemCollection();
            }


            // make a connection to the database and execute the SQL stored procedure
            // to retrieve the listing of URL items in the form
            // [Name], [UrlTemplate], [Redirection]

            using (SqlConnection con = new SqlConnection(GetConnectionString()))
            {
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = _procName;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet        ds = new DataSet();
                    da.Fill(ds);
                    da.Dispose();

                    if (ds.Tables.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            DataRow        r = ds.Tables[0].Rows[i];
                            UrlMappingItem item
                                = UrlMappingHelper.CreateTemplatedMappingItem(
                                      r["Name"].ToString(), r["UrlTemplate"].ToString(), r["Redirection"].ToString(), _qsBehavior
                                      );

                            _coll.Add(item);
                        }
                    }
                }
            }

            // if we're using a sqlDependency, generate it now
            if (_useDependency)
            {
                _sqlDependency = new SqlCacheDependency(_dependencyName, _tableName);
                HttpContext.Current.Cache.Insert(kCACHE_KEY, "dummyValue", _sqlDependency, DateTime.Now.AddDays(10), Cache.NoSlidingExpiration);
            }

            // remember the refresh time
            _latestRefresh = System.DateTime.Now;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a <see cref="UrlMappingItem" /> given a name, templated URL string, and redirection string.
        /// </summary>
        /// <param name="name">A name for this mapping item</param>
        /// <param name="templatedUrl">A template for URLs to be matched</param>
        /// <param name="redirection">The redirection that the UrlMappingModule should apply when incoming URLs match the template.</param>
        /// <param name="qsBehavior">defines how the UrlMappingModule should treat querystring values on an incoming URL for pattern matching; this is typically provided through declarative configuration</param>
        /// <returns>The created <see cref="UrlMappingItem" /></returns>
        /// <remarks>
        /// <para>
        /// The template URL string is relative to the web application and may or may not include
        /// the "~/" characters at the beginning.  The template may indicate an exact URL to match,
        /// for example <i>Reports/Summary.aspx</i> or may include token names surrounded
        /// by [SquareBrackets] for dynamic matching, such as <i>Reports/[ID]/[Action].aspx</i>.
        /// When dynamic templates are matched in runtime by the UrlMappingModule, dynamic tokens
        /// are appended to the redirection string as querystring items,
        /// such as <i>?ID=<i>xxx</i>&amp;Action=<i>xxx</i></i>.
        /// </para>
        /// <para>
        /// The redirection string may be an absolute URL, such as <i>http://www.microsoft.com</i>,
        /// a server-relative URL beginning with a "/", such as <i>/AnotherAppOnThisServer/page.aspx</i>,
        /// or an application-relative URL to a concrete resource, optionally beginning with a "~/", such as
        /// <i>~/ReportSummary.aspx</i> or <i>ReportSummary.aspx</i>.
        /// </para>
        /// </remarks>
        public static UrlMappingItem CreateTemplatedMappingItem(string name, string templatedUrl, string redirection, IncomingQueryStringBehaviorEnum qsBehavior)
        {
            UrlMappingItem item = new UrlMappingItem(name, CreateTemplatedMappingRegex(templatedUrl, qsBehavior), redirection);

            return(item);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Refreshes the internally-cached collection of URL templates and redirection mappings.
        /// </summary>
        protected void RefreshUrlMappingData()
        {
            if (_coll != null)
            {
                _coll.Clear();
            }
            else
            {
                _coll = new UrlMappingItemCollection();
            }


            // parse the given xml file to retrieve the listing of URL items;
            // the xml file should include tags in the form:
            // <urlMapping name="" urlTemplate="" redirection="" />

            XmlDocument xml  = new XmlDocument();
            string      file = HttpContext.Current.Server.MapPath(_urlMappingFile);

            try
            {
                xml.Load(file);
            }
            catch (Exception ex)
            {
                throw new ProviderException("There is an XmlUrlMappingModule error.  The error occurred while loading the urlMappingFile.  A virtual path is required and the file must be well-formed.", ex);
            }

            // parse the file for <urlMapping> tags
            XmlNodeList nodes = xml.SelectNodes("//urlMapping");

            foreach (XmlNode node in nodes)
            {
                // retrieve name, urlTemplate, and redirection attributes;
                // ensure urlTemplate and redirection are present
                string name        = XmlAttributeValue(node, "name");
                string urlTemplate = XmlAttributeValue(node, "urlTemplate");
                string redirection = XmlAttributeValue(node, "redirection");
                bool   enabled     = XmlAttributeBoolean(node, "enabled", true);

                if (enabled)
                {
                    if (string.IsNullOrEmpty(urlTemplate))
                    {
                        throw new ProviderException("There is an XmlUrlMappingModule error.  All <urlMapping> tags in the mapping file require a 'urlTemplate' attribute.");
                    }

                    if (string.IsNullOrEmpty(urlTemplate))
                    {
                        throw new ProviderException("There is an XmlUrlMappingModule error.  All <urlMapping> tags in the mapping file require a 'redirection' attribute.");
                    }

                    // still here, we can create the item and add to the collection
                    UrlMappingItem item
                        = UrlMappingHelper.CreateTemplatedMappingItem(
                              name, urlTemplate, redirection, _qsBehavior
                              );

                    _coll.Add(item);
                }
            }



            // if we're using a file dependency, generate it now
            if (_useDependency)
            {
                _fileDependency = new CacheDependency(file);
                HttpContext.Current.Cache.Insert(kCACHE_KEY, "dummyValue", _fileDependency);
            }

            // remember the refresh time
            _latestRefresh = System.DateTime.Now;
        }