using Microsoft.AspNetCore.Http;
namespace zero.Core.Extensions
{
public static class HttpContextExtensions
{
///
/// Whether the current request is a backoffice request
///
public static bool IsBackofficeRequest(this HttpContext context, string backofficePath)
{
string path = backofficePath.EnsureStartsWith('/').TrimEnd('/');
return context.Request.Path.ToString().StartsWith(path);
}
///
/// Whether the current request is an AJAX request
///
public static bool IsAjaxRequest(this HttpContext context)
{
if (context?.Request?.Headers == null)
{
return false;
}
return context.Request.Headers["X-Requested-With"] == "XMLHttpRequest";
}
///
/// Whether the current request only accepts application/json
///
public static bool IsJsonRequest(this HttpContext context)
{
if (context?.Request?.Headers == null)
{
return false;
}
return context.Request.Headers["Accept"] == "application/json";
}
}
}