Skip to content

Instantly share code, notes, and snippets.

@ramazankanbur
Created December 21, 2021 18:25
Show Gist options
  • Save ramazankanbur/08f68b9f8caa962a12af17e58a8722ee to your computer and use it in GitHub Desktop.
Save ramazankanbur/08f68b9f8caa962a12af17e58a8722ee to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Mvc.Filters;
using Wangkanai.Detection.Services;
namespace Mobile_Redirection_Subfolder
{
public class DesktopRedirectAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var path = filterContext.HttpContext.Request.Path.ToString();
if (!IsMobile())
{
path = path.Contains("/mobile/") ? path.Substring(7) : path;
filterContext.HttpContext.Response.Redirect(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