How do I detect a mobile browser in a .NET MVC3 application

后端 未结 3 930
一个人的身影
一个人的身影 2020-12-04 15:14

I\'m developing a .NET MVC3 application.

Is there a good way to detect if the user is using a mobile browser in the view (using RAZOR). I\'m wanting to differ the

相关标签:
3条回答
  • 2020-12-04 15:55

    MVC3 exposes an IsMobileDevice flag in the Request.Browser object.

    So in your razor code, you can query this variable and render accordingly.

    For example, in your view (razor):

    @if (Request.Browser.IsMobileDevice) {
      <!-- HTML here for mobile device -->
    } else {
      <!-- HTML for desktop device -->
    }
    
    0 讨论(0)
  • 2020-12-04 16:04

    The built in browser detection capabilities are no longer being kept up to date. Take a look at Scott Hanselman's blog - refer to the "More to Come" section at the bottom for details.

    From that article:

    Since that post, the Live.com team in Ireland that released and supported the original Mobile Device Browser File (MDBF) has stopped producing it. The best source for mobile browser device data is WURFL (that was one of the places that MDBF pulled from.)

    I suggest taking a look at 51Degrees.mobi for more accurate detection. Also see the Steve Sanderson blog that Hanselman references for how to implement this in MVC3.

    0 讨论(0)
  • 2020-12-04 16:08

    I use this Method (Works fine for Me)

    if (eDurar.MobileDetect.DeviceType.Any(m => Request.UserAgent.Contains(m)))
    {
        Layout = "~/Views/Shared/_mobileLayout.cshtml";
        @Html.Partial("mobileIndex");
    
    }
    else
    {
        Layout = "~/Views/Shared/_Layout.cshtml";
        @Html.Partial("desktopIndex");
    }
    

    I suggest you to use responsive bootstrap something, avoiding mobile specific page is better

    0 讨论(0)
提交回复
热议问题