Link to home
Create AccountLog in
Avatar of ToddBeaulieu
ToddBeaulieuFlag for United States of America

asked on

Accessing Application object from controller

New to web development. Having a bit of "flow confusion".

I set an Application value in global.asax that identifies the environment (dev/test/prod).

I have a base controller that I want to expose a "CanUserEdit" property to be used by controllers, and also set an Application value for views to consume.

In my master page, I test "ViewData["CanUserEdit"]", which explodes because there is no such value.

I can't set the ViewData value in the base controller's constructor because there's no HttpContext at that point.

So, where can I hook into to set the ViewData value and how will this tie in with unit testing? I guess I'll need to be able to hand over a mock context for testing.

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Kumaraswamy R
Kumaraswamy R
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of ToddBeaulieu

ASKER

Actually, the solution turned out to be setting the ViewData in the Initialize event of the controller.

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            SetViewData();
        }

I'll give you the solution, though, because it was actually an interesting writeup. Thank you.