Skip to main content

C# extension methods for the HttpContextBaseExtensions class.

using System;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Routing;

namespace Extensions
{
    public static class HttpContextBaseExtensions
    {
        /// <summary>
        /// Gets a value indicating whether we're running under Linux or a Unix variant.
        /// </summary>
        /// <value><c>true</c> if Linux/Unix; otherwise, <c>false</c>.</value>
        public static bool IsLinux(this HttpContextBase instance)
        {
            int platform = (int)Environment.OSVersion.Platform;
            if (platform == 4)
            {
                return true;
            }
            return platform == 128;
        }

        /// <summary>
        /// Gets a value indicating whether we're running under Mono.
        /// </summary>
        /// <value><c>true</c> if Mono; otherwise, <c>false</c>.</value>
        public static bool IsMono(this HttpContextBase instance)
        {
            return Type.GetType("Mono.Runtime") != null;
        }

        /// <summary>
        /// Requests the context.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns></returns>
        public static RequestContext RequestContext(this HttpContextBase instance)
        {
            return new RequestContext(instance, RouteTable.Routes.GetRouteData(instance) ?? new RouteData());
        }
    }
}