Snippets

Surinder Bhomra UrlExtensions

You are viewing an old version of this snippet. View the current version.
Revised by Surinder Bhomra b717e9d
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace SurinderBhomra.Common.Extensions
{
    public static class UrlExtensions
    {
        /// <summary>
        /// Checks if the link is an internal or external link.
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public static bool IsExternalLink(this string link)
        {
            return link != string.Empty && link.ToLower().StartsWith("http://") || link.ToLower().StartsWith("https://");
        }

        /// <summary>
        /// Get domain from current http context.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetCurrentDomain(this HttpContext context)
        {
            context = HttpContext.Current.GetOrCreateNewInstance();

            if (context != null)
                return $"{HttpContext.Current.Request.Url.Scheme}{Uri.SchemeDelimiter}{HttpContext.Current.Request.Url.Host}{(!HttpContext.Current.Request.Url.IsDefaultPort ? $":{HttpContext.Current.Request.Url.Port}" : null)}";
            
            return string.Empty;
        }
    }
}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.