ASP.NET Developer Notes

Ryan Garaygay's ASP.NET notes online

Hand Editing Web.Config or not

I've come across a number of issues in the past where the cause of the issue is that someone hand edited the web.config and resulted to a not well formed XML (missing tags, unclosed quotes et al).

The recommended way of doing it is to minimize these issues is to use the ASP.NET Configuration Settings Dialog which is available to IIS (not sure if there is a stand alone executable for this). Here are simple steps to do it.

1. Open IIS

2. Right Click on target site/virtual directory and select Properties

 

3. The Properties dialog should show up. Select ASP.NET tab.

 

4. At the ASP.NET tab, you should see and click Edit Configuration. (if you're opening Properties of a website, you should also see an "Edit Global Configuration" button which corresponds to the machine wide web.config). Doing so should display the Configuration Settings Dialog

 

5. Make your modifications without fear of unclosed quotes whatsoever. You could likely still mess up some settings but I can assure you that it would be easier to do when you're manually hand editing the file.

 

Also note that when adding a web.config in Visual Studio (using Add New Item) you will get a comment somewhere at the top stating: 

<!--

    Note: As an alternative to hand editing this file you can use the

    web admin tool to configure settings for your application. Use

    the Website->Asp.Net Configuration option in Visual Studio.

    A full list of settings and comments can be found in

    machine.config.comments usually located in

    \Windows\Microsoft.Net\Framework\v2.x\Config

-->


The "Website->Asp.Net Configuration option in Visual Studio" mentioned are items in the toolbar and appears if you have the website project (or its files) open/selected (see image below)

This is also a recommended way of doing it for web site projects (not available in Web Application Projects).

Editing web.config is likely inevitable for development but as much as possible I would recommend not hand-edit them unless you (or others in the team) really have to (especially for those with not much experience with XML/HTML - it is trivial but could minimize some headaches)

UPDATE: I have updated the post title from "Hand Editing web.config, not" to "Hand Editing web.config or not" since it seems to make a big difference. I personally hand edit most if not all web.config I work with but this is merely an alternative based on experience where there are actually people out there guilty of messing the xml for a lot of reasons. Being an alternative it may or may not be best for all situations. Also, as a starting/budding developer it is sometimes (if not all the times) best to learn from mistakes so I am in no way against hand editing of files. However like gurus out there, be careful in doing as it would probably hurt more than missing something in HTML. Furthermore for those not yet confident enough to mess up with config manually this might be for you. Although again it's best to learn from mistakes if you can afford to do so since you will surely remember it more. :)

Posted: Apr 22 2008, 09:49 AM by ryangaraygay | with 11 comment(s)
Filed under:

Comments

Dave said:

Exactly how does a web.config make it through at least 2 other environments before getting to your production without noticing a problem? Problem doesn't sound like hand editing rather editing files directly in production.

# April 22, 2008 12:28 AM

ryangaraygay said:

So far I've only seen this in non-production environments but in cases where there is only one other environment than production, it might still be possible.

Your right. But more likely both, 1.) editing files directly in production and 2) hand-editing them. Both of which could cause a headache on its own.

As mentioned, maybe not be an issue to experienced developers and those following good development processes but I won't be surprised if a number of those doing development ran into to this one way or another. :)

# April 22, 2008 4:56 AM

pho said:

and... does this work if you make custom configuration handlers, too? or only for basic string appSettings

# April 22, 2008 7:17 AM

Peter said:

I always hand-edit my web.configs. A few days ago I tried to move a site that ran fine in IIS6 to IIS7 under Vista (I hope it's fixed in the real IIS7). When I used the tool to edit my web.config, guess what... it corrupted the file. It didn't leave any open tags, but it didn't parse it well and duplicated some branches which gave me runtime errors. Took me a while to figure out, originally I didn't think the tool would mess up the file... there goes your advice.

# April 22, 2008 8:34 AM

ryangaraygay said:

@pho: I'm not sure what you're referring to with custom configuration handlers but if it's customer configuration sections, I don't think it would be able to handle that. In addition to appsettings though, you could also specify settings for connectionstrings, authorization, authentication, state management etc. The later (Website -> ASP.NET Configuration) also enables you to manage your membership, role providers.

@Peter: thanks for the feedback. I haven't had the chance to try it out with IIS7 (haven't worked with Vista at all in fact) but thanks for the significant observations.

Most of the time I personally hand edit the config files too but as mentioned tools like these are made to minimize errors in addition to ease of configuration. If they mess up then it sucks and should be fixed. It might be a good idea (not to mention highly appreciated by the community) if you could provide such information to say Microsoft Connect too. When I get a chance to try it out, I'll be sure to update. :)

# April 22, 2008 11:30 AM

.NET Developer Notes said:

Not Hand editing web.config

# April 22, 2008 11:36 AM

mikedopp said:

I constantly hand edit my web.config... What? My site is down? Crap gotta go!

# April 23, 2008 11:29 AM

propecia said:

I want to say - thank you for this!

# June 6, 2009 3:40 PM

alhadass said:

AWSOMENESS!!!!!

thank for helping a brotha out dude!

# August 20, 2009 10:03 AM

turbo said:

BEST MAAAAN :D THANK YOU

# September 15, 2009 12:25 PM

hotmom said:

Thanks so much! I've looked everywhere for this! You're amazing!

# September 27, 2009 10:02 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)