1

I opened an existing ASP.NET MVC3 project that formerly worked just fine in a newly built machine with Visual Studio 2012 installed.

I'm now getting null exceptions in the following code. After a User logs in, the following code is then executed to perform page fragment-level access.

The null exception is thrown on the WebViewPage.User object. However, when in debug mode, I can actually inspect a real instance of the object (see image below)

 public static class WebViewPageExtensions
 {
      public static bool HasAccess(this WebViewPage pg)
        {
            if (pg == null) throw new ArgumentNullException("WebViewPage is null");
            if (pg.User == null) throw new ArgumentNullException("WebViewPage User is null");              
            return pg.User.IsInRole(User.ACCESS_LEVEL_A) || pg.User.IsInRole(User.ACCESS_LEVEL_B);
        }
 }

Note: this could be something with the way my environment is configured (web.config maybe?), but not really sure where to start. As it is, the code that this is happening on is the same code running in production and the former developer environment.

All the references are still referring to the ASP.NET MVC3 version of the framework running on the .NET 4.0 platform.

Inspecting the "null" pg.User object

EDIT: Here's the callilng code from a Razer view:

 @if (this.HasAccess()) {                 
        <div>
                HTML here
        </div>  
    }

Any thoughts?

8
  • Can you show the code that is calling HasAccess? Commented Oct 3, 2012 at 16:09
  • Added an edit above that has the HasAccess() call. It's in a Razor template Commented Oct 3, 2012 at 16:41
  • Ok, and how about your controller action? does the User object exist in the controller before you render the view? And are you using a custom principal? Commented Oct 3, 2012 at 16:44
  • The User object is just the RolePrincipal. I can view it in the controller via HttpContext.User. Commented Oct 3, 2012 at 16:56
  • 1
    Well, there's nothing inherently wrong here.. so the problem must be somewhere else. Unfortunately, these kinds of problems when you upgrade can be caused by things that are unrelated to the actual problem. All you can do is start whittling down things until the problem goes away, then slowly adding things back until you discover the actual problem. Commented Oct 3, 2012 at 17:09

1 Answer 1

1

Please read section "Installing ASP.NET MVC 4 breaks ASP.NET MVC 3 RTM applications" from link http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253815. Follow setps "Required updates" to update your MVC 3 app.
If you havent noticed, MVC4 gets installed with VS2012 and this is documented behavior.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, this didn't originally work for me back when I tried it in October. However, now in December, it seems to work in the Visual Studio 2012, Update 1. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.