/// <summary> /// Accepts a configuration object from the <see cref="UrlMappingModule"/> /// and initializes the provider. /// </summary> /// <param name="config"> /// the configuration settings typed as a <c>UrlMappingProviderConfiguration</c> object; /// the actual object type may be a subclass of <c>UrlMappingProviderConfiguration</c>. /// </param> void IUrlMappingProvider.Initialize(UrlMappingProviderConfiguration config) { // cast the configuration object provided by the module SqlUrlMappingProviderConfiguration sqlConfig = (config as SqlUrlMappingProviderConfiguration); if (sqlConfig == null) { throw new ProviderException("Invalid SqlUrlMappingProvider configuration. Check the web.config file settings."); } // remember configuration settings _connectionString = sqlConfig.ConnectionStringName; _procName = sqlConfig.ProcName; _tableName = sqlConfig.TableName; _useDependency = sqlConfig.UseDependency; _dependencyName = sqlConfig.DependencyName; _qsBehavior = sqlConfig.IncomingQueryStringBehavior; // if useDependency is set, make sure a dependency name is also if (_useDependency && string.IsNullOrEmpty(_dependencyName)) { throw new ProviderException("Invalid SqlUrlMappingProvider configuration. If 'useDependency' is true, you must supply a 'dependencyName'. Check the web.config file settings."); } // initialize the url mappings RefreshUrlMappingData(); }
/// <summary> /// Accepts a configuration object from the <see cref="UrlMappingModule"/> /// and initializes the provider. /// </summary> /// <param name="config"> /// the configuration settings typed as a <c>UrlMappingProviderConfiguration</c> object; /// the actual object type may be a subclass of <c>UrlMappingProviderConfiguration</c>. /// </param> void IUrlMappingProvider.Initialize(UrlMappingProviderConfiguration config) { // cast the configuration object provided by the module XmlUrlMappingProviderConfiguration xmlConfig = (config as XmlUrlMappingProviderConfiguration); if (xmlConfig == null) { throw new ProviderException("Invalid XmlUrlMappingProvider configuration. Check the web.config file settings."); } // remember configuration settings _urlMappingFile = xmlConfig.UrlMappingFile; _useDependency = xmlConfig.UseDependency; _qsBehavior = xmlConfig.IncomingQueryStringBehavior; // initialize the url mappings RefreshUrlMappingData(); }
/// <summary> /// Return a regular expression translated from the given templated URL. /// </summary> /// <param name="templatedUrl">A URL pattern relative to the application, with [TokenNames] included for dynamic matching</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>A regular expression as a <see cref="Regex" /> object</returns> /// <remarks> /// <para> /// A templated URL uses token names surrounded by [SquareBrackets] to provide for dynamic /// matching and parsing. /// For example, the URL template: /// <i>Reports/[ID]/[Action].aspx</i> /// translates to the following regular expression: /// <i>Reports/(?<ID>[a-zA-Z0-9_+%]*)/(?<Action>[a-zA-Z0-9_+%]*).aspx"</i> /// </para> /// <para> /// The regular expression is then used by the UrlMappingModule to match incoming URLs. /// If a match is found, token names and values are automatically appended to the /// redirection URL as querystring arguments. So, given the above templated URL string /// of <i>Reports/[ID]/[Action].aspx</i> with a Redirection defined as /// <i>ReportAction.aspx</i>, an incoming URL of <i>Reports/47/Edit.aspx</i> /// is redirected by the UrlMappingModule to the following: /// <i>ReportAction.aspx?ID=47&Action=Edit</i>. /// </para> /// </remarks> public static Regex CreateTemplatedMappingRegex(string templatedUrl, IncomingQueryStringBehaviorEnum qsBehavior) { // return a case-insensitive regex that must match the url completely return(CreateTemplatedMappingRegex(templatedUrl, false, true, qsBehavior)); }
/// <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>&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); }
/// <summary> /// Return a regular expression translated from the given templated URL. /// </summary> /// <param name="templatedUrl">A URL pattern relative to the application, with [TokenNames] included for dynamic matching</param> /// <param name="caseSensitive"><i>true</i> if the returned expression should be case sensitive, <i>false</i> if not</param> /// <param name="matchCompletely"><i>true</i> if the returned expression should include beginning and ending anchors, ensuring that a match will require the complete URL and not a portion of it</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>A regular expression as a <see cref="Regex" /> object</returns> /// <remarks> /// <para> /// A templated URL uses token names surrounded by [SquareBrackets] to provide for dynamic /// matching and parsing. /// For example, the URL template: /// <i>Reports/[ID]/[Action].aspx</i> /// translates to the following regular expression: /// <i>Reports/(?<ID>[a-zA-Z0-9_+%]*)/(?<Action>[a-zA-Z0-9_+%]*).aspx"</i> /// </para> /// <para> /// The regular expression is then used by the UrlMappingModule to match incoming URLs. /// If a match is found, token names and values are automatically appended to the /// redirection URL as querystring arguments. So, given the above templated URL string /// of <i>Reports/[ID]/[Action].aspx</i> with a Redirection defined as /// <i>ReportAction.aspx</i>, an incoming URL of <i>Reports/47/Edit.aspx</i> /// is redirected by the UrlMappingModule to the following: /// <i>ReportAction.aspx?ID=47&Action=Edit</i>. /// </para> /// </remarks> public static Regex CreateTemplatedMappingRegex(string templatedUrl, bool caseSensitive, bool matchCompletely, IncomingQueryStringBehaviorEnum qsBehavior) { // return a regex that parses an app-relative url string and constructs an appropriate // Regular Expression for matching it; the template may include tokens in the form // of [TokenItem] which are matched as groups; // e.g. the url template "Reports/Display/[ID].aspx" becomes the regular expression: // Reports/Display/(?<ID>[a-zA-Z0-9_+%]*).aspx" // if matchCompletely is true, the regex is anchored; if false, the match can occur // anywhere within the url // options for the returned regular expression RegexOptions options = (caseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase); // if the templatedUrl contains a querystring ? and we're incorporating querystring values in the regex //then escape it if (qsBehavior == IncomingQueryStringBehaviorEnum.Include) { templatedUrl = templatedUrl.Replace("?", "\\?"); } // escape any periods or plusses in the templatedUrl templatedUrl = templatedUrl.Replace(".", "\\."); templatedUrl = templatedUrl.Replace("+", "\\+"); // use a regex now to parse for [Tokens] Regex r = new Regex(@"(\[[a-zA-Z0-9_]*\])"); MatchCollection matches = r.Matches(templatedUrl); string s = templatedUrl; foreach (Match m in matches) { string tokenName = m.Value.Substring(1, m.Value.Length - 2); string replacement = string.Format("(?<{0}>[a-zA-Z0-9_ \\(\\)\\-\\+\\'\\\"\\.]+)", tokenName); s = s.Replace(m.Value, replacement); } // if the matchCompletely option is desired, add anchors to the regex string if (matchCompletely) { s = "^(/){0,1}" + s + "$"; } // now construct and return the regular expression to test for these kinds of URLs return(new Regex(s, options)); }
private void Initialize() { // get the configuration section UrlMappingProviderConfiguration config = (UrlMappingProviderConfiguration)ConfigurationManager.GetSection(kMAPPINGMODULE); if (config == null) { throw new ProviderException("The configuration section for the UrlMappingModule is missing from Web.config."); } if (!string.IsNullOrEmpty(config.ProviderTypeString)) { // instantiate a concrete provider given the provider type string through reflection Type t = Type.GetType(config.ProviderTypeString); // if the type couldn't be retrieved, it could be because the user has it defined in APP_CODE; // try the BuildManager if (t == null) { t = BuildManager.GetType(config.ProviderTypeString, false, true); } // if we still have an error, throw the exception if (t == null) { throw new ProviderException("Cannot locate the type '" + config.ProviderTypeString + "' for the UrlMappingModule. Check your web.config settings."); } // attempt to instantiate the provider given its type _provider = (IUrlMappingProvider)Activator.CreateInstance(t); } else { _provider = null; } if (_provider == null) { throw new ProviderException("Invalid provider for UrlMappingModule. This must be a type that implements IUrlMappingProvider. Check your web.config settings, section 'urlMappingModule', attribute 'providerType'"); } // remember other configuration properties _noMatchAction = config.NoMatchAction; _noMatchRedirectPage = config.NoMatchRedirectUrl; _automaticallyUpdateFormAction = config.AutomaticallyUpdateFormAction; _qsBehavior = config.IncomingQueryStringBehavior; _processingEvent = config.UrlProcessingEvent; _authorizeRedirectionUrl = config.AuthorizeRedirectionUrl; _authorizeFailureRedirectUrl = config.AuthorizeFailureRedirectUrl; // test configuration combinations if (_authorizeRedirectionUrl == true && _processingEvent != UrlProcessingEventEnum.AuthorizeRequest) { throw new ConfigurationErrorsException("When the <urlMappingModule> 'authorizeRedirectionUrl' attribute is 'true', the 'processEvent' attribute must be 'AuthorizeEvent'."); } // remember the list of extensions to ignore _ignoreExtensions = config.IgnoreExtensions.Split(new char[] { ' ', ';', ',' }); for (int i = 0; i < _ignoreExtensions.Length; i++) { _ignoreExtensions[i] = _ignoreExtensions[i].Trim().ToLower(); } // allow the provider to initialize itself given the configuration section if (_provider != null) { _provider.Initialize(config); } }