سؤال

Hi This is My action filter, I need to detect the size of the screen to redirect to the adequate action, How can do this ??

public sealed class DetectViewFilterAttribute : ActionFilterAttribute
        {
            private readonly IRegistrationConfiguration _registrationConfiguration;
            public DetectViewFilterAttribute()
            {
                _registrationConfiguration = DependencyResolver.Current.GetService<IRegistrationConfiguration>();
            }
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                bool isMobile = false;
                string userAgent = HttpContext.Current.Request.UserAgent.ToLower();
                Regex mobileDetectionRegularExpression = new Regex(_registrationConfiguration.DetectMobileRegularExpression);

                isMobile = mobileDetectionRegularExpression.IsMatch(userAgent);
                if (isMobile)
                {
                    String url;
                    UrlHelper helper = new UrlHelper(filterContext.RequestContext);

                      // TODO **if width de device between 300 and 600 px*
                      url = helper.Action("Mobile","Inscription");
                      else
                       url = helper.Action("Tablette","Inscription");

                           HttpContext.Current.Response.Redirect(url);

                }
                base.OnActionExecuting(filterContext);
            }

}

هل كانت مفيدة؟

المحلول

You cannot know screen size on server side.. BUT you can know the user agent and then know it is a tablet, PC or smartphone. Then, you are closer to determine which view to display.

As you are using MVC4... it is wise to read this article: http://www.hanselman.com/blog/MakingASwitchableDesktopAndMobileSiteWithASPNETMVC4AndJQueryMobile.aspx

This way, you don't re-invent the wheel... as this behaviour is built in the asp.net MVC framework.

نصائح أخرى

Sadly, you can't have server side code detect client screen width alone. You'll need something on the client side (probably Javascript) to send the server the information about the client's window exact size. This is a little tough on the user experience since a page has to load first, then send that information to the server, then the server can react. That's why user agent sniffing is used a lot of time since that information is sent to the server on each request.

And as @Murali said, there's also solutions like using CSS Media Queries which will allow you to do different CSS rules based on client width without needing to involve the server.

Unfortunately on the server, there is no direct way to get the size of the screen. You can only do this on the client using JavaScript.

However, there are files that .Net uses to determine certain mobile capabilities. These are located in C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\Browsers. However, since screen sizes can be different for the same mobile client, they are not defined there.

As the other guys wrote, you can't do that server side. But, if you like to have this information only for mobile device WURFL has that information (resolution_width and resolution_height).

Check the specs here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top