user1987162
user1987162

Reputation:

Check if a mobile device is requesting the desktop site

I'm using 51 degrees on my MVC site as an adaptive approach to serving my pages. All is working well.

I have recently added output caching and I use the VaryByCustom to check whether it is mobile or desktop:

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        // this is for the output cache
        if (context != null)
        {
            switch (custom)
            {
                case "Mobile":
                    return GetMobileCustomString(context);
            }
        }

        return base.GetVaryByCustomString(context, custom);
    }

    private static string GetMobileCustomString(HttpContext context)
    {
        if (context.Request.Browser.IsMobileDevice)
        {
            return "IsMobile";
        }
        else
        {
            return "IsDesktop";
        }
    }

However I have run into a problem that if the first mobile user to browse the site has requested the desktop version, this will be cached for all mobile users.

I need to change the GetMobileCustomString to include a check for if it is a mobile requesting a desktop site. Is there any way to do this?

Update

As a bounty has been opened on this, I thought I would offer an update:

Firstly, it was not the first load causing the desktop page to be cached as I had initially thought, so having done a lot of searching, research and testing on this, I feel the desktop page should never be cached for the mobile version (if you are on MVC 5). I have stepped through the code when doing a mobile request and when it gets to the vary by custom, it shows context.Request.Browser.IsMobileDevice as false.

Not sure what is causing the desktop site to be cached for the mobile site - perhaps it is a hangover from the bug in MVC 4. It just seems to be random (ie, one day it will be fine and then another day it will be serving the desktop site for some reason) and recycling the app pool will always fix it.

I also found that I could get the overriden browser by using:

using System.Web.Wepages;

context.Request.RequestContext.HttpContext.GetOverriddenBrowser();

But it didn't seem to be much use

Upvotes: 6

Views: 1744

Answers (2)

Zarwalski
Zarwalski

Reputation: 69

Within the 51degrees.config there is a setting

 <redirect devicesFile="" timeout="20" firstRequestOnly="true"

This firstrequestonly may be confusing your caching system so treating the first mobile device as the first request, and all other mobiles as the same session of the original mobile device. Try setting this to false and see if that helps solve your issue.

Upvotes: 0

JuanR
JuanR

Reputation: 7803

It seems to me like what you are doing should work, provided you are storing the cache at the client side.

You should be using the OutputCache attribute on your controller actions in the following manner:

[OutputCache(Duration = 5, VaryByCustom = "Mobile", Location = OutputCacheLocation.Client)]

(The duration is up to you)

That being said, browser detection is based on HTTP headers so nothing is going to help you if the requesting browser is sending headers for a different agent.

The other option is to use feature detection instead.

I hope that helps.

Upvotes: 0

Related Questions