로그인 할 때 액션이 발생하면 해당 액션이 모바일 장치에 있는지 확인한 다음 모바일 로그인 페이지로 리디렉션됩니다. 그런 다음 액션
private static string[] mobileDevices = new string[] {"iphone","ppc",
"windows ce","blackberry",
"opera mini","mobile","palm",
"portable","opera mobi" };
public static bool IsMobileDevice(string userAgent)
{
// TODO: null check
userAgent = userAgent.ToLower();
return mobileDevices.Any(x => userAgent.Contains(x));
}
:
public ActionResult Index()
{
if (MobileHelper.IsMobileDevice(Request.UserAgent))
{
// Send to mobile route.
return RedirectToAction("Login", "MobileActivation");
}
// Otherwise do normal login
}
편집 :이 광범위하게 응용 프로그램이 다음을 수행 할 수를 전체의 적용하고자한다면
.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class RedirectToMobileArea : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var rd = filterContext.HttpContext.Request.RequestContext.RouteData;
var currentAction = rd.GetRequiredString("action");
var currentController = rd.GetRequiredString("controller");
string currentArea = rd.Values["area"] as string;
if (!currentArea.Equals("mobile", StringComparison.OrdinalIgnoreCase) && MobileHelper.IsMobileDevice(Request.UserAgent))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", currentController }, { "action", currentAction }, { "area", "mobile" } });
}
}
}
이 필터는 경우 조치 점검을 모바일 (그리고 이미 모바일 영역에서)와 같은 행동과 컨트롤러로 전송됩니다
이 같은 어디서든 뭔가를 적용 할 수있는 ActionFilter 만들기 모바일 영역. 참고 당신이 그런 see this answer
당신과 같은 각 작업에 필터를 적용 할 수 중 하나는 컨트롤러 네임 스페이스와 경로를 등록해야합니다 같은 이름의 컨트롤러와 함께 갈 경우 :
[RedirectToMobileArea]
public ActionResult Index()
{
// do stuff.
}
또는 경우
[RedirectToMobileArea]
public abstract class BaseController : Controller {
}
를 다음에 : 당신은 모든 지역 모든 컨트롤러에서 상속하고 그에 적용됩니다 BaseController를 생성하고 싶은 그것에서 헤리 트가 :
덕분에이 내가 갔다하는 방식이었다 ...
나는이 모든 테스트 havent 한하지만 작동합니다. 내가 그것을하고 싶었던 방식으로 가능한지 알고 싶었을 것이다. –
하나의 옵션으로 수정 사항을 추가하겠습니다. – shenku
끝내 주셔서 감사합니다! –