Created
December 21, 2021 18:25
-
-
Save ramazankanbur/08f68b9f8caa962a12af17e58a8722ee 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 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