ASP.NET MVC NullReferenceException when inheriting from a Base Controller

Posted by rockinthesixstring on Stack Overflow See other posts from Stack Overflow or by rockinthesixstring
Published on 2010-06-15T03:46:55Z Indexed on 2010/06/15 3:52 UTC
Read the original article Hit count: 230

I've got a base controller that I inherit all of my Controllers from. It's job is to basically set caching and error handling as well as check for mobile browsers.

My UI works fine, but my Unit Tests are failing.

Imports System.Web.Mvc

<HandleError()> _
<CompressFilter()> _
<OutputCache(Duration:=30, VaryByParam:="id")> _
Public Class BaseController : Inherits System.Web.Mvc.Controller

    Protected Overrides Function View(ByVal viewName As String, ByVal masterName As String, ByVal model As Object) As System.Web.Mvc.ViewResult
        Dim ismobile As Nullable(Of Boolean) = Request.Browser.IsMobileDevice

        If ismobile Then
            Return MyBase.View(viewName, "Mobile", model)
        Else
            Return MyBase.View(viewName, "Site", model)
        End If

    End Function

End Class

The error I'm getting in my Unit test is on Dim ismobile As Nullable(Of Boolean) = Request.Browser.IsMobileDevice saying

Object Reference Not Set To An Instance Of An Object.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about nullreferenceexception