3

Is there a way to setup some default settings for webpages being ran on debug mode?

For example, I'd like to set a default session userid on debug mode, but trying this in Application_Start() causes errors:

protected void Application_Start()
{
     #if DEBUG
     Session["User"] = "1"
     #endif
}

I could instead choose to place that in controllers, but that'd require a lot of code duplication.

3 Answers 3

3

The reason that this throws an error is because the Session object is associated with a request. There is no request in the Application_Start event. You could move this logic to the Application_AcquireRequestState event handler and it would work fine.

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        if (HttpContext.Current.IsDebuggingEnabled)
        {
            HttpContext.Current.Session["user"] = "1";
        }
        else
        {
            HttpContext.Current.Session["user"] = "0";
        } 
    }

EDIT

After further research, I was mistaken. In the .NET pipeline, the above event handler is the first place that you can access the session object. It has not been retrieved from the server in the Application_BeginRequest which is why you are getting a NullReferenceException still. I tested the above code in MVC 5 and had no issues.

Also, instead of having to rely on compiler constants, you can easily tell if you are in debug mode or not using the above syntax of HttpContext.Current.IsDebuggingEnabled. This basically is a wrapper for checking the web.config attribute on the compilation tag and seeing if debug="true"

Note - You have to use the fully qualified name of HttpContext.Current.Session (as above) since the global event handlers do not have a direct inheritance of the current HttpContext.

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

3 Comments

Weird, session is always null with the above approach, even with the fully qualified name
@l46kok - you're right! I looked into and tested this a little further. See if my edits achieve what you need in an MVC project. Added a little explanation as well.
This event doesn't fire for some reason on any of the page requests
3

You could use the Conditional attribute and put it on a method in your base controller. Something like this:

public class BaseController : Controller {
    public BaseController() {
        SetupDebugData();
    }

    [Conditional("DEBUG")]
    public void SetupDebugData() {
        Session["User"] = "1";
    }
}

The call to SetupDebugData will be optimized away for Release builds. Then you just inherit from this controller in your other controllers:

public class AdminController : BaseController {
    // ...
}

Comments

2

Yes, off-course.

  public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

#if(DEBUG)
            Console.WriteLine("This is debug mode");
#else
            Console.WriteLine("This is release mode");
#endif
            base.OnActionExecuting(filterContext);
        }

This code will be work in MVC ( I use it since MVC3). Remember that you need to set release build and debug false in web.config when you deploy your asp.net app on server.

1 Comment

This requires duplicate code in Views now instead of controllers.

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.