Snippets

Surinder Bhomra UrlExtensions

Created by Surinder Bhomra last modified
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;
        }
    }
}

Comments (0)