TryCreate() public static method

public static TryCreate ( Uri baseUri, Uri relativeUri, Uri &result ) : bool
baseUri Uri
relativeUri Uri
result Uri
return bool
Example #1
0
        Uri CreateUriForCookies(string url)
        {
            if (url == null)
            {
                return(null);
            }

            Uri uri;

            if (url.Length > 2000)
            {
                url = url.Substring(0, 2000);
            }

            if (Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                if (String.IsNullOrWhiteSpace(uri.Host))
                {
                    return(null);
                }

                return(uri);
            }

            return(null);
        }
Example #2
0
        /// <summary>
        ///     Render the Html in the same way that the DefaultViewService does
        /// </summary>
        /// <param name="model"></param>
        /// <param name="page"></param>
        /// <param name="html"></param>
        /// <param name="clientName"></param>
        /// <returns></returns>
        private Task <Stream> Render(
            CommonViewModel model,
            string page,
            string html,
            string clientName = null)
        {
            Uri uriSiteUrl;
            var applicationPath = string.Empty;

            if (Uri.TryCreate(model.SiteUrl, UriKind.RelativeOrAbsolute, out uriSiteUrl))
            {
                if (uriSiteUrl.IsAbsoluteUri)
                {
                    applicationPath = uriSiteUrl.AbsolutePath;
                }
                else
                {
                    applicationPath = uriSiteUrl.OriginalString;
                    if (applicationPath.StartsWith("~/"))
                    {
                        applicationPath = applicationPath.TrimStart('~');
                    }
                }

                if (applicationPath.EndsWith("/"))
                {
                    applicationPath = applicationPath.Substring(0, applicationPath.Length - 1);
                }
            }

            var json = JsonConvert.SerializeObject(
                model,
                Formatting.None,
                new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            var additionalStylesheets = this.BuildTags(
                "<link href='{0}' rel='stylesheet'>",
                applicationPath,
                this.config.Stylesheets);
            var additionalScripts = this.BuildTags("<script src='{0}'></script>", applicationPath, this.config.Scripts);

            var variables = new
            {
                siteName = Encoder.HtmlEncode(model.SiteName),
                applicationPath,
                model = Encoder.HtmlEncode(json),
                page,
                stylesheets = additionalStylesheets,
                scripts     = additionalScripts,
                clientName
            };

            html = Replace(html, variables);

            return(Task.FromResult(this.StringToStream(html)));
        }
    bool isAbsoluteUrl(string url)
    {
        Uri result;

        return(Uri.TryCreate(url, System.UriKind.Absolute, out result));
    }
Example #4
0
        void InitService()
        {
            Log(LogLevel.Info, "Initializing server");

            if(!HttpListener.IsSupported)
                throw new ApplicationException("HttpListener is not supported on this version of Windows.");
            if(string.IsNullOrWhiteSpace(Host))
                Host = "+";
            if(this.Port <= 0 || this.Port > 65535)
                throw new ArgumentException("Port must be greater than 0 and less than 65535");

            listener.Prefixes.Clear();
            listener.Prefixes.Add(string.Format("http://{0}:{1}/", this.Host, this.Port));
            Uri = new System.Uri(listener.Prefixes.First().Replace("+", "localhost"));

            Log(LogLevel.Info, "Obtaining service method information");

            bridges = from mi in GetType().GetMethods(_bindingFlags).OfType<MethodInfo>()
                      let attribs = mi.GetCustomAttributes(false).OfType<VerbAttribute>()
                      where attribs.Count() > 0
                      select new ServiceBridge
                      {
                          MethodInfo = mi,
                          AttributeCount = attribs.Count(),
                          Attribute = attribs.First()
                      };

            foreach(var method in bridges.Where(m => m.AttributeCount > 1))
            {
                Log(LogLevel.Warning, "{0} has multiple VerbAttributes, defaulting to '{1}' method.", method.QualifiedName, method.Attribute.Verb);
            }

            foreach(var method in bridges.Where(m => m.MethodInfo.ContainsGenericParameters))
            {
                Log(LogLevel.Warning, "{0} contains generic parameters which are not currently supported.", method.QualifiedName);
            }

            Log(LogLevel.Info, "Validating placeholder variables");
            var bads = bridges.Where(m => m.InvalidPlaceholders().Count() > 0);
            if(bads.Count() > 0)
            {
                foreach(var b in bads)
                {
                    Log(LogLevel.Error, "Invalid placeholder(s) on the {0} method: {1}", b.MethodInfo.Name, string.Join(",", b.InvalidPlaceholders()));
                }
                throw new InvalidPlaceholderException(bads.First().MethodInfo.Name, bads.First().InvalidPlaceholders());
            }

            if(AllowDescribe)
            {
                Uri temp;
                if(Uri.TryCreate(Uri, DescribePath, out temp))
                    DescriptionUri = temp;
            }
        }
Example #5
0
        void InitService()
        {
            Log(LogLevel.Info, "Initializing server");

            if (!HttpListener.IsSupported)
            {
                throw new ApplicationException("HttpListener is not supported on this version of Windows.");
            }
            if (string.IsNullOrWhiteSpace(Host))
            {
                Host = "+";
            }
            if (this.Port <= 0 || this.Port > 65535)
            {
                throw new ArgumentException("Port must be greater than 0 and less than 65535");
            }

            listener.Prefixes.Clear();
            listener.Prefixes.Add(string.Format("http://{0}:{1}/", this.Host, this.Port));
            Uri = new System.Uri(listener.Prefixes.First().Replace("+", "localhost"));

            Log(LogLevel.Info, "Obtaining service method information");

            bridges = from mi in GetType().GetMethods(_bindingFlags).OfType <MethodInfo>()
                      let attribs = mi.GetCustomAttributes(false).OfType <VerbAttribute>()
                                    where attribs.Count() > 0
                                    select new ServiceBridge
            {
                MethodInfo     = mi,
                AttributeCount = attribs.Count(),
                Attribute      = attribs.First()
            };

            foreach (var method in bridges.Where(m => m.AttributeCount > 1))
            {
                Log(LogLevel.Warning, "{0} has multiple VerbAttributes, defaulting to '{1}' method.", method.QualifiedName, method.Attribute.Verb);
            }

            foreach (var method in bridges.Where(m => m.MethodInfo.ContainsGenericParameters))
            {
                Log(LogLevel.Warning, "{0} contains generic parameters which are not currently supported.", method.QualifiedName);
            }

            Log(LogLevel.Info, "Validating placeholder variables");
            var bads = bridges.Where(m => m.InvalidPlaceholders().Count() > 0);

            if (bads.Count() > 0)
            {
                foreach (var b in bads)
                {
                    Log(LogLevel.Error, "Invalid placeholder(s) on the {0} method: {1}", b.MethodInfo.Name, string.Join(",", b.InvalidPlaceholders()));
                }
                throw new InvalidPlaceholderException(bads.First().MethodInfo.Name, bads.First().InvalidPlaceholders());
            }

            if (AllowDescribe)
            {
                Uri temp;
                if (Uri.TryCreate(Uri, DescribePath, out temp))
                {
                    DescriptionUri = temp;
                }
            }
        }
        public static bool IsValdiUrl(this string str, bool allowEmpty = true)
        {
            if (allowEmpty && string.IsNullOrEmpty(str))
            {
                return(true);
            }

            return(!string.IsNullOrWhiteSpace(str) && str.Contains(".") && str.IndexOf('.') < (str.Length - 1) && str.StartsWith("http", StringComparison.Ordinal) && Uri.IsWellFormedUriString(str, UriKind.Absolute) && Uri.TryCreate(str, UriKind.Absolute, out _));
        }