private static string CreateQueryStringUrlTemplate(string functionId, XmlNamespaceSearchCriterionSet xmlCriterionSet, HttpContext httpContext, IOpenSearchContext openSearchContext)
        {
            UriBuilder uriTemplate = new UriBuilder(openSearchContext.BaseSearchUrlTemplate.ToString().Replace("{functionId}", functionId));

            uriTemplate.Query = xmlCriterionSet.ToQueryStringUrlTemplate();

            return(uriTemplate.ToString());
        }
        public static OpenSearchDescription GenerateOpenSearchDescription(ISearchEngine searchEngine, HttpContext httpContext, IOpenSearchContext openSearchContext)
        {
            OpenSearchDescription openSearchDescription = new OpenSearchDescription();

            openSearchDescription.Url = OpenSearchHelpers.CreateOpenSearchDescriptionUrls(searchEngine.GetSearchFunctions(), httpContext, openSearchContext).ToList();

            return(openSearchDescription);
        }
        /// <summary>
        /// This function creates a list of opensearch Url with templates from a list
        /// of search functions based on the formatting capabities of the httpContext
        /// </summary>
        /// <param name="searchFunctions">list of search functions to make an opensearch Url template from</param>
        /// <param name="httpContext">the HTTP context</param>
        /// <returns></returns>
        public static IEnumerable <OpenSearchDescriptionUrl> CreateOpenSearchDescriptionUrls(IDictionary <string, ISearchFunction> searchFunctions, HttpContext httpContext, IOpenSearchContext openSearchContext)
        {
            // prepare an empty url list
            List <OpenSearchDescriptionUrl> urls = new List <OpenSearchDescriptionUrl>();

            // get the OpenSearch Formatter selector
            OpenSearchFormatterSelector selector = (OpenSearchFormatterSelector)httpContext.RequestServices.GetService(typeof(OpenSearchFormatterSelector));

            // urls are the combination of the search function possible results type and the relevant formatters
            foreach (var searchFunction in searchFunctions)
            {
                foreach (var formatter in selector.SelectFormatters(searchFunction.Value.ResultType))
                {
                    // if the formatter is able to serialize the result, then there is a possible url template
                    var supportedContentTypes = formatter.GetSupportedContentTypes(null, searchFunction.Value.ResultType);
                    if (supportedContentTypes == null || supportedContentTypes.Count == 0)
                    {
                        continue;
                    }
                    var xmlCriterionSet          = QualifyCriterionSetForOpenSearch(searchFunction.Value.SearchCriterionSet);
                    OpenSearchDescriptionUrl url = new OpenSearchDescriptionUrl(
                        supportedContentTypes.First(),
                        CreateQueryStringUrlTemplate(searchFunction.Key, xmlCriterionSet, httpContext, openSearchContext),
                        "results",
                        xmlCriterionSet.GetExtraNamespacesIfAny()
                        );
                    urls.Add(url);
                }
            }

            return(urls);
        }