简体   繁体   中英

Where to put the redirect code for mobile devices in Asp.net MVC

I got the detection code for C# from http://detectmobilebrowsers.com/ . Now, I don't know where to put this up and how to get it working.

Basically, all I want is that my mobile visitors should be redirected to http://m.site.com , where my mobile website resides. Let me know how to achieve this task via above code or any other method.

NOTE :- The main website is an Asp.net MVC3 application.

I would call the code in the global.asax Application_BeginRequest event.

void Application_BeginRequest(object sender, EventArgs e)
{
    string u = Request.ServerVariables["HTTP_USER_AGENT"];
    if (BrowserDetect.IsMobile(u)) //Pretend there is class and function that has all the regex stuff here.
    {
        Response.Redirect("http://m.site.com");
    }
}

Well you can write an asp.net HTTP Module in your main mvc3 app. In the module handle the BeginRequest event and put your detection code in there. If it's a mobile request then redirect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM