Created
December 21, 2021 18:27
-
-
Save ramazankanbur/4f43d5baf0dec1d8d287cf4cbf74a8b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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