HOWTO: Manage multiple web.configs without going insane

Most ASP.NET set-ups encourage an environment with three webservers : production, testing, and development. In optimal solutions, all the servers are exactly the same, but in real life, they aren’t, and it causes problems. So, you end up having to adjust your web.config differently for each server.

Some suggest using the build tools with your IDE to tell it which files to put where, what to put in files, etc, but this requires scripting and work. Who has time to learn yet another language for that? Instead, I propose an easy solution.

web.config Managed Gracefully

Keep all of your configs inside the solution, with filenames indicative of the servers they go on. Of course, web.config which will carry the contents needed for the current server. Upon every deployment, I simply swap the contents of web.config with the correct config for the project’s location. Simple. Picture below.

Multiple web.configs managed gracefully

Multiple web.configs managed gracefully

Cannot Find App/Folder/App/Login.aspx

System.Web.HttpException: The file ‘/Application/Folder/Application/Login.aspx’ does not exist.

Thanks to Rick Strahl, I figured it out. Was using Forms Authentication and it looked like this:

      <forms loginUrl="Login.aspx"
         protection="All"
         timeout="60"
         name="Application"
         path="/"
         requireSSL="false"
         slidingExpiration="true"
         defaultUrl="default.aspx"
         cookieless="AutoDetect" />

So we are using Forms Authentication and trying to get the application to redirect to the login page whenever the user is not logged in. The problem? In a folder it tries to redirect to Application/Folder/Application/Login.aspx , which of course exists somewhere else. To fix it, I took out the Path and put in a nice ~ in the RedirectURL. It now performs correctly:

      <forms loginUrl="~/Login.aspx"
         protection="All"
         timeout="60"
         name="Application"
         requireSSL="false"
         slidingExpiration="true"
         defaultUrl="default.aspx"
         cookieless="AutoDetect" />

ASP.NET Specific Error Message Fixes

The files ‘MyScripts.js’ and ‘CodeBehind.cs’ use a different language, which is not allowed since they need to be compiled together.

This error occurred when I put a JavaScript file in my App_Code folder. Simply move it out of that folder.

Both DataSource and DataSourceID are defined on ‘(id)’. Remove one definition.

This is an odd error message that usually means that you are binding a control twice to a datasource. However, if you are binding your control to an object (datatable, for example), that that object is empty and contains no data. Populate your data object and the error message will go away.

 

(I’ll post more as I find them…)

ASP.NET Apps : Migrating IIS 6 to IIS 7 ; IIS 7 Errors

Migrating from IIS 6 to IIS 7 is a pain in the rear.

From first hand experience, let me tell you, you want to start with a new web.config generated by Visual Studio. There’s a few new sections in there like runtime, and system.webServer. The latter being specifically for IIS7.

I experienced a plethoria of issues in this migration process with my ASP.NET applications. The primary symptoms of IIS 6 to IIS 7 migration troubles are pages that work on some servers but not others, in some browsers but not others, and mysteriously work one day but not another. The primary culprit is usually the web.config.

Here are some issues I discovered and how to fix them.

httpHandlers and httpModules

Server Error in Application “”
Error Summary: HTTP Error 500.22 – Internal Server Error
An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.

Most Likely Causes: This application defines configuration in the system.web/httpModules section.
Most Likely Causes: This application defines configuration in the system.web/httpHandlers section.

FIX: IIS7 does not seem to like httpModules and httpHandlers. In actuallity, it does like them, very much so, however, they are now renamed to ‘modules’ and ‘handlers’ respectively and belong in the system.webServer. You can delete the sections, but then you will probably encounter errors with Validation controls not being called automatically upon postbacks – and you can work around that by manually calling Page.Validate() and if (!Page.IsValid){ return } .

Clientside Javascript (Custom Validators):

I was manually entering “(document.findbyid(‘$ctx200$section1$uxTextbox3’) …” and this had problems with IIS7 that were fine under IIS6. Try using uxTextbox.ClientID instead of hardcoding IDs.

Assemblies Errors

Could not load file or assembly ‘System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.

FIX: The new IIS7 web.config section runtime includes some new ways to use assemblies that include an ‘oldversion’ tag, they look like this:

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
          <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
          <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
      </assemblyBinding>
    </runtime>

OR, you can do it the hard way: going to START > RUN > assembly And adding the correct version and public key tokens. If it is already correct and you still get errors, try changing the string to all upper or all lower case letters.

Forms Authentication Problems

SYMPTOMS : The terrible white screen of IE death – “Internet Explorer cannot display this webpage”, no error messages. Inside your Event Log, there will probably be 302 Error messages. Some other symptoms include the page taking over five minutes to load in IE and the IE process taking upwards of 1,800,000 of memory and substantially high CPU from your server (mine was at 55%!).

FIX: The new IIS7 targeted web.config has sections that fix this.

Javascript ScriptHandler/ScriptManager Errors

‘Sys’ is undefined

SYMPTOMS: Custom Javascript and/or CustomValidator can start acting funny or skip validation. Sometimes a page works on one server but not another, in IE but not Firefox, etc, etc.

This can occur when the  web.config does not contain all of the sections for AJAX. Scrutinize your web.config line by line and make sure all of the AJAX sections are included. In my case, I was missing some sections inside of the system.webServer section, and after re-addition, everything works beautifully again.

 

Related Posts: web.config Hell: Managing Multiple or Mixed Environment web.config’s