/// <summary>
        /// Converts an instance of a markup extension to formatted output text.
        /// If any exceptions or error conditions occur (like not finding the
        /// requested markup extenstion processor by name) error output is returned
        /// instead.  Under no circumstances should this method raise an exception
        /// up to the calling code.  The only thing not protected by the try/catch
        /// are calls to GetErrorOutput which is designed to not throw exceptions.
        /// </summary>
        /// <param name="name">The tag name of the markup extension, e.g. HtmlHelpers.Date.</param>
        /// <param name="parameters">The parameters specified in the markup extension tag.</param>
        /// <returns>Formatted text to replace the markup extension.</returns>
        private string Process(string name, string[] parameters)
        {
            string output = String.Empty;

            try
            {
                string nameLowered = name.ToLower(); // Keys are normalized to be all lower case.
                if (_handlers.ContainsKey(nameLowered) == true)
                {
                    Func <MarkupExtensionHandler> getHandlerDelegate = _handlers[nameLowered];
                    MarkupExtensionHandler        handler            = getHandlerDelegate();
                    output = handler.Process(parameters);
                }
                else
                {
                    output = GetErrorOutput(String.Format("Unknown markup extension.  No markup extension with the name \"{0}\" has been loaded.", name));
                }
            }
            catch (MarkupExtensionException mex)
            {
                // Catch any MarkupExtensionExceptions in the code above or from any handlers and output the error response according to "returnHandlerErrorsAsOutput" configuration.
                output = GetErrorOutput(mex.Message);
            }
            catch (Exception ex)
            {
                // Do the same thing as for a MarkupExtensionException for now but in the future when we have better logging, may want to do something different here.
                output = GetErrorOutput(ex.Message);
            }

            return(output);
        }
Esempio n. 2
0
        /// <summary>
        /// Allows derived classes to add handlers to the internal _handlers dictionary,
        /// normalizing the markup extension name to be lower case.
        /// </summary>
        /// <param name="name">
        /// The name of the markup extension as used in the tag, e.g. HtmlHelpers.Date.
        /// </param>
        /// <param name="handlerDelegate">
        /// A delegate that returns an instance of a MarkupExtensionHandler capable of processing
        /// the named markup extension.
        /// </param>
        protected void Add(Func <MarkupExtensionHandler> handlerDelegate)
        {
            // Instantiate an instance to get its name.
            MarkupExtensionHandler handler = handlerDelegate();
            string name = handler.Name;

            // Add the delegate to the dictionary, keyed by name.
            _handlers.Add(name.ToLower(), handlerDelegate);
        }