Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I usually try to figure things out myself but I've been working on this problem for over a week and I'm not making any headway :(.

I created an HTTP Module that detects the user's browser agent and redirects them if they are coming from a mobile browser. I'm using v2.0 in IIS integrated mode. I installed the DLL in the GAC and everything seems to work for the first 1000 or so requests but after that it suddenly stops working. I just can't seem to figure out why and I think I'm missing something basic in my C# knowledge. I would truly be grateful for any help I can get.

 public class RedirectModule : System.Web.IHttpModule
    {
        private const string MobileUserAgent = "MobileUserAgentCacheKey";

        private const string STRING_TO_FIND = "info/lps";
        private const string STRING_TO_ADD = "/mobile";


        public void Dispose()
        {
            //clean-up code here.
        }

        public void Init(HttpApplication context)
        {
          context.BeginRequest += context_BeginRequest;

        }

        private void context_BeginRequest(object sender, EventArgs e)
        {

            try
            {


                HttpContext context = HttpContext.Current;


                string url = context.Request.Url.ToString().ToLower();

                if (!url.Contains(STRING_TO_FIND) || url.Contains(STRING_TO_FIND + STRING_TO_ADD))
                    return;
                Logger.Current.Log("Starting Redirect Phase");

                if (XmlToValues.IsMobile(context.Request.ServerVariables["HTTP_USER_AGENT"],
                                       GetCachedFile(context, "Settings.xml")))
                {
                    var mobileRedirect = GetRedirectUrl(url, STRING_TO_FIND, STRING_TO_ADD);
                    if (mobileRedirect != null)
                    {
                        Logger.Current.Log("Redirect to Mobile page");
                        context.Response.Redirect(mobileRedirect);

                    }
                }
                Logger.Current.Log("Web Page");
                Logger.Current.Log("End Begin Request");
            }
            catch (Exception ex)
            {
                if (ex is ThreadAbortException)
                    return;

                Logger.Current.LogError(ex);
                // HandleError(ex);
            }
        }


        public static string GetRedirectUrl(string url, string strToFind, string strToAdd)
        {
            try
            {
                Logger.Current.Log("Get Redirect Url ");
                int idx = url.IndexOf(strToFind) + strToFind.Length;
                return url.Substring(0, idx) + strToAdd + url.Substring(idx);
            }
            catch (Exception ex)
            {

                Logger.Current.LogError(ex);

                //  HandleError(ex);
                return null;
            }
        }


        private XmlNodeList GetCachedFile(HttpContext context, string filePath)
        {
            try
            {
                Logger.Current.Log("GetCachedFile START");
                if (context.Cache[MobileUserAgent] == null)
                {
                    context.Cache[MobileUserAgent] = XmlToValues.GetMobileUserAgents(filePath);
                    Logger.Current.Log("Add Mobile File to Cache");
                }
                return (XmlNodeList)context.Cache[MobileUserAgent];
            }
            catch (Exception ex)
            {
                Logger.Current.LogError(ex);
                //  HandleError(ex);
                return null;
            }
        }
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.