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