TtT23
TtT23

Reputation: 7030

ASP.NET MVC Debug-mode settings

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.

Upvotes: 3

Views: 6172

Answers (3)

Anirudha Gupta
Anirudha Gupta

Reputation: 9279

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.

Upvotes: 2

Tommy
Tommy

Reputation: 39807

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.

Upvotes: 3

Simon Whitehead
Simon Whitehead

Reputation: 65049

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 {
    // ...
}

Upvotes: 3

Related Questions