Tuesday, December 19, 2006

How to get Intellisense for Web.config and App.config in Visual Studio .NET

Overview

Web.config and App.config have can have many different configuration options but it is difficult to remember what some of the less commonly used options are. This article provides a schema definition for .NET configuration files such as Web.config and App.config. The schema makes Visual Studio .NET help you out by displaying intellisense when working in these files.

Extend Visual Studio .NET with XML Schema

Visual Studio .NET has an extensible model that provides intellisense in XML and HTML-based documents based on XML schema. To get intellisense for Web.config or App.config I created a schema for the .NET configuration files and and put the schema into a folder under the Visual Studio .NET installation.

Visual Studio .NET then reads the schema file and lists the schema in the targetNamespace drop-down list when editing the properties of the document. Selecting the schema from the list adds the xmlns attribute to the root element of the document.

Warning!

There is a problem with this method that you need to be aware of. To get intellisense in Visual Studio .NET, the xmlns attribute is added to the root element of the document so that Visual Studio .NET knows which schema to use for the document.

ASP.NET and Winforms applications will throw a runtime error if the element in web.config or app.config has the xmlns attribute when you run your application. e.g. This will give an error.



You must remember to remove the xmlns attribute when you are finished using intellisense in your config file. If you do not, you will get an error in your application!