コード例 #1
0
        /// <summary>
        /// Will return the best matching content type OR the first given!
        /// </summary>
        /// <param name="myContentTypes"></param>
        /// <returns></returns>
        public ContentType GetBestMatchingAcceptHeader(params ContentType[] myContentTypes)
        {
            var    _ListOfFoundAcceptHeaders = new List <AcceptType>();
            UInt32 pos = 0;

            foreach (var _ContentType in myContentTypes)
            {
                var _AcceptType = new AcceptType(_ContentType.ToString(), pos++);

                var _Match = AcceptTypes.Find(_AType => _AType.Equals(_AcceptType));

                if (_Match != null)
                {
                    if (_Match.ContentType.GetMediaSubType() == "*") // this was a * and we will set the quality to lowest
                    {
                        _AcceptType.Quality = 0;
                    }

                    _ListOfFoundAcceptHeaders.Add(_AcceptType);
                }
            }

            _ListOfFoundAcceptHeaders.Sort();

            if (!_ListOfFoundAcceptHeaders.IsNullOrEmpty())
            {
                return(_ListOfFoundAcceptHeaders.First().ContentType);
            }
            else if (!myContentTypes.IsNullOrEmpty())
            {
                return(myContentTypes.First());
            }
            else
            {
                return(null);
            }
        }