alexk
alexk

Reputation: 1574

Turn CustomErrors off and on quickly for production and development

I develop a lot of ASP.NET web forms. During testing, I have customErrors off, then I have a ResponseRedirect custom error for when I move the form into production.

I'm wondering if I can simplify my development by just changing a setting value to accomplish this.

What I would like is to just change my Properties.Settings.Default.TestMode value from developer to production and have this turn on the customError.

So I was thinking my Global.asax.cs code could look like:

protected void Application_Error(object sender, EventArgs e) {
  if (Properties.Settings.Default.TestMode == "production") {
    Server.ClearError();
    Response.Redirect("errorpage.htm");
  } 
  else {
    // Not sure what it would do here. Nothing?
  }
}

But then I'm not sure what would go in my Web.config file. CustomErrors is shown below.

<!--<customErrors mode="Off" />-->
<customErrors mode="On" defaultRedirect="ErrorPage.htm" redirectMode="ResponseRedirect"/>

Right now I just comment out/in what I need to to make the change. I feel like I do this all the time and it would be great to just have the TestMode setting that I've created do all the work.

Upvotes: 2

Views: 1249

Answers (1)

Shai Cohen
Shai Cohen

Reputation: 6249

You want to look into Web.Config Transformation. It allows you to set specific settings for development and production independently.

Here is a good walk through from Microsoft: How to: Transform Web.config When Deploying a Web Application Project

Upvotes: 1

Related Questions