T4MVC for Web.config <appSettings>
The good thing about T4MVC is that it allows you to get rid of literal / magic strings .
T4MVC is a T4 template for ASP.NET MVC applications that creates typed helpers that eliminate the use of literal strings when accessing controllers, actions, and views.
I'm just wondering if it's possible to have something like this when it comes to application settings inside the Web.config
file:
<appSettings> <add key="SecurityGuardEmailFrom" value=" info@email.net " /> <add key="PasswordExpiresInDays" value="1" /> <add key="NumberOfPasswordsToKeep" value="5" /> </appSettings>
So instead:
private static readonly int PasswordExpiresInDays = int.Parse(ConfigurationManager.AppSettings["PasswordExpiresInDays"]);
We would have something like this:
MVC.Webconfig.PasswordExpiresInDays
or
MVC.AppSettings.PasswordExpiresInDays
This will help at compile time to verify that the application parameter still exists, avoiding runtime errors.
Will it even be viable? If yes, did you know that there something similar to this is implemented somewhere?
Well, interestingly enough, after I posted a question that I was looking for with different words and found something that turned out there: T4 template that has good built-in error handling ... Here is the message:
T4 template for accessing AppSettings in configuration files
I had to modify the T 4 code template provided in the message to work in my current environment (VS 11 Beta + ASP.NET MVC 4).
How to use?
Download the AppSettings.tt
T4 file and put it in the root of your ASP.NET MVC, for example. Include the file in your project and right-click it and select the Run Custom Tool
, and you're done. A new class called AppSettings
will be available with all the settings for your application. HEALTHY! :)
This is the import I use:
<#@ template language="C#" debug="true" hostspecific="true" #> <#@ output extension=".cs" #> <#@ Assembly Name="System.Core.dll" #> <#@ assembly name="EnvDTE" #> <#@ Assembly name="System.Configuration"#> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Diagnostics" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Specialized"#> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Configuration" #>
Some application parameters in Web.config
have a colon (:) in their names, such as webpages:Version
. This is what I did to make it work:
public static string <#=setting.Replace(":", "")#> { get { return getConfigSetting("<#=setting#>"); } }
Pay attention to setting.Replace
above.
If you want, you can also debug the T4 template. Just follow the steps described here:
Tiny Happy Features # 1 - Debugging a T4 Template in Visual Studio 2012
Create a custom configuration section instead of setting your preferences in <appSettings>
.
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
You can extend your ASP.NET configuration options with the XML configuration of your own elements. To do this, you create a custom configuration section handler. The handler must be a .NET Framework class that inherits from the System.Configuration.ConfigurationSection class. the section handler interprets and processes the parameters that are defined in the XML configuration elements in a specific section of the Web.config file. You can read and write these settings through the property handler.