As a .NET developer, I really like ASP.NET MVC web framework. It has a lot of extension points.

Just now I have a task to integrate mobile website to existing website. So instead of having a single responsive view, we have 2 separate views. Mobile browser will display the jQuery mobile version.

ASP.NET MVC by default doesn’t accept action method overloading unless if the method has ActionMethodSelectorAttribute which is useful to determine the action selection. One of its implementation is AcceptVerbsAttribute class, which is used to determine that a specific action only responds to the specified HTTP verb.

So in my case, I have an action which only makes sense for mobile view.

public class AcceptClientAttribute : ActionMethodSelectorAttribute
{
    private readonly ClientType clientType;

    public AcceptClientAttribute(ClientType clientType)
    {
        this.clientType = clientType;
    }

    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        switch(clientType)
        {
            case ClientType.Mobile:
                return controllerContext.RequestContext.HttpContext.Request.Browser.IsMobileDevice;

            default:
                return !controllerContext.RequestContext.HttpContext.Request.Browser.IsMobileDevice;
        }
         
    }
}

public enum ClientType
{
    Desktop,
    Mobile
}

The usage is just like action filter, so we apply it to action's attribute.
FYI, for above example I use 51Degrees.mobi for device detection. It automatically overrides Request.Browser property.