Skip to content

Instantly share code, notes, and snippets.

@ramazankanbur
Created December 21, 2021 18:27
Show Gist options
  • Save ramazankanbur/4f43d5baf0dec1d8d287cf4cbf74a8b5 to your computer and use it in GitHub Desktop.
Save ramazankanbur/4f43d5baf0dec1d8d287cf4cbf74a8b5 to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Mvc.Filters;
using Wangkanai.Detection.Services;
namespace Mobile_Redirection_Subfolder
{
public class MobileRedirectAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var path = filterContext.HttpContext.Request.Path.ToString();
if (IsMobile())
{
filterContext.HttpContext.Response.Redirect("/mobile" + path);
}
bool IsMobile()
{
var service = (DetectionService)filterContext.HttpContext.RequestServices.GetService(typeof(IDetectionService));
var deviceType= service.Device.Type;
return deviceType == Wangkanai.Detection.Models.Device.Mobile;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment