One of the cool features in Visual Studio 2010 in the new web.config transform functionality for “automagically” changing the values in your web.config file based on your deployment scenario.
I’ve been working with this new feature now for a bit and just ran into an issue that was completely unexpected. In one of my web applications I have an application setting that hold a filepath. When I moved to using the web.config transform I encountered a bug where extra linebreaks and whitespace gets inserted into the body of the Value node for a given application setting.
Here’s an example of what you might have in you web.config file to begin with:
1: <configuration>
2: ...
3: <applicationSettings>
4: <MyApp.Properties.Settings>
5: <setting name="LocalFilePath" serializeAs="String">
6: <value>C:\temp</value>
7: </setting>
8: </MyApp.Properties.Settings>
9: </applicationSettings>
10: ...
11: </configuration>
And lets say that you have the following in your Web.Release.config transform file.
1: <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
2: <applicationSettings>
3: <MyApp.Properties.Settings>
4: <setting name="LocalFilePath" serializeAs="String" xdt:Transfrom="Replace" xdt:Locator="Match(name)">
5: <value>D:\MyAppWebRoot\Files</value>
6: </setting>
7: </MyApp.Properties.Settings>
8: </applicationSettings>
9: </configuration>
After you publish the site and examine the the changes to your web.config file you will discover it looks like this:
1: <configuration>
2: ...
3: <applicationSettings>
4: <MyApp.Properties.Settings>
5: <setting name="LocalFilePath" serializeAs="String">
6: <value>
7: D:\MyAppWebRoot\Files
8: </value>
9: </setting>
10: </MyApp.Properties.Settings>
11: </applicationSettings>
12: ...
13: </configuration>
The resulting extra linebreaks and whitespace will cause all kinds of problems in your code if you don’t modify your code to trim the value of the property. So, what can we do to fix this problem? Well, you have one of three options:
- Add “.Trim” to your code in every place you use an application settings of type string. (What the heck! Who wants to do that!?!)
- Forget about using the config transform until SP1 is release with a fix and just do it all manually. (Uh…have I ever mentioned how much I don’t like doing that now?)
- Modify the “Settings” class that is auto generated so that it trims the value of any application setting of type string. (BINGO! I’ll take option three please!)
So, if your like me and you want option three then here is the code in both C# and VB that will get you what you want. All you really need to do is create a code file in your project and drop the appropriate code in it with the right namespace defined.
BTW, if you want to vote to have this fixed then here is the issue on the Microsoft Connect website.
C#
namespace MyApp.Properties
{
internal sealed partial class Settings
{
public override object this[string propertyName]
{
get
{
string value;
if ((value = base[propertyName] as string) != null) { return value.Trim(); }
return base[propertyName];
}
set { base[propertyName] = value; }
}
}
}
Visual Basic.NET
Namespace My
Partial Friend NotInheritable Class MySettings
Default Public Overrides Property Item(ByVal propertyName As String) As Object
Get
If MyBase.Item(propertyName) IsNot Nothing Then
If TypeOf MyBase.Item(propertyName) Is System.String Then Return MyBase.Item(propertyName).ToString.Trim
End If
Return MyBase.Item(propertyName)
End Get
Set(ByVal value As Object)
MyBase.Item(propertyName) = value
End Set
End Property
End Class
End Namespace
Tags: ASP.NET, web.config transform, Visual Studio 2010