ReWrite in web.config to go between mobile and desktop sites

佐手、 提交于 2019-12-11 10:55:50

问题


I am finishing up a new mobile website to compliment the desktop version. Currently I have a rewrite rule in the web.config file on my desktop site that looks like so:

<system.webServer>
<rewrite>
  <rules>
    <rule name="MobileRedirect" patternSyntax="ECMAScript" stopProcessing="true">
      <match url=".*" ignoreCase="true" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_COOKIE}" pattern="nomobile" ignoreCase="true" negate="true" />
        <add input="{HTTP_USER_AGENT}" pattern="android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos" />
      </conditions>
      <action type="Redirect" url="http://m.mysite.com" appendQueryString="false" redirectType="Found" />
    </rule>
  </rules>
</rewrite>

This works perfect if I never want the user to be able to use the desktop site while on a mobile device, but that is not always the case. Some links on the mobile site DO link back into the desktop version. I have a link on the footer of the mobile site to "view full site" as well.

So my question IS: how to properly handle cookie setting on that link and then detecting in the web.config and NOT redirecting to the mobile version IF it exists.... I have a conditional check in the web.config for http_cookie "nomobile", but I don't think it is properly working. Do I just send in a querystring value from the mobile and check that in the global.asax file or does that not work because the web.config runs first?

The desktop is a C# MVC4 site on IIS 7.5 if any of that helps, and the mobile site is a simply jquery mobile site.

Thank you!

EDIT: I have tried checking the querystring in the global.asax file (code below) but it seems that "Request is not available in this context".

// create and set cookie if ?nomobile detected
        string forcedesktop = HttpContext.Current.Request["nomobile"];
            if(forcedesktop != null){
                HttpCookie nomobile = new HttpCookie("nomobile");
                Request.Cookies.Add(nomobile);
            }
        }

来源:https://stackoverflow.com/questions/18765043/rewrite-in-web-config-to-go-between-mobile-and-desktop-sites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!