Archive for March, 2009

Creating Custom Configuration Settings

March 25, 2009

The .NET framework has several options to offer when it comes to storing configuration settings in your configuration file.  When storing simple key-value settings, the AppSettings section may be all that you need.  However, in other cases, you might want to store more complex configuration settings.  If that is the case, creating your own, custom configuration section may be a better option.

.NET Framework 2.0 allows you to create a class that inherits from the ConfigurationSection class, which makes creating a custom configuration section a piece of cake.  The class I am showing here as an example is not very spectacular, but show the most important features.  The class represents settings that might be used for a form in your application that allows users to contact you.  This example assumes that all mail from your application will be sent to one, configurable email address.  It also assumes that you might want to prefix the subject (as entered by the user) with a short prefix, so you can use it for easy filtering in your mail client.

First, you need to create the class that will be used to retrieve the configuration settings from te configuration file:

namespace CustomConfigurationDemo
  public class MailFormConfiguration : ConfigurationSection
      IsRequired = true)]
    public string MailTo
        return (String)this["mailTo"];
        this["mailTo"] = value;
      DefaultValue = "",
      IsRequired = false)]
    public string SubjectPrefix
        return (String)this["subjectPrefix"];
        this["subjectPrefix"] = value;

The important things to note here, are:

  • The class inherits from ConfigurationSection;
  • Each property has a ConfigurationProperty attribute

For the ConfigurationProperty attribute to work, you must specify the name of the attribute in the coniguration file that will be used to retrieve the value.  The ConfigurationProperty attribute also allows you to specify a DefaultValue, and you can use IsRequired to indicate whether the attribute must be included in the configuration file.  If you set IsRequired to true and the attribute is not found in the configuration file, a ConfigurationErrorsException will be thrown.

In order to use this MailFormConfiguration class, you will need to include the following in your configuration file:

<?xml version="1.0" encoding="utf-8" ?>
    <section name="mailFormConfiguration"
                   CustomConfigurationDemo" />
  <mailFormConfiguration mailTo="" />

As you can see, the subjectPrefix attribute is not required, and is not included in the configuration file.  Its value will be set to the default value specified using the ConfigurationProperty attribute which, in this case, is an empty string.

The following code fragment shows how the custom configuration settings class can be used in your application:

MailFormConfiguration config =
string mailTo = config.MailTo;
string subjectPrefix = config.SubjectPrefix;

If you found this post helpful, please click below to “Kick” it:

kick it on


ASP.NET MVC RTM Release Available

March 19, 2009

A moment a lot of people have been waiting for has arrived: ASP.NET MVC RTM has been released!

Read more about the release on Phil Haack’s blog.  You can also find some extra information about last minute changes (mostly to T4 templates) here: Visual Web Developer Team Blog.

And most important of all, links to the new goodies:

ASP.NET MVC 1.0 download (The Futures download can be found on the same page).

ASP.NET MVC 1.0 source code download

Have fun with the new toys. 🙂

If you found this post helpful, please click below to “Kick” it:

kick it on

ASP.NET MVC RenderPartial Compiler Error

March 4, 2009

Am I the only person who can stare at a piece of code for ten minutes and not notice the obvious?  I was trying to use the HtmlHelper.RenderPartial method tonight, using the code fragment below:

<%= Html.RenderPartial(
    "~/Views/Shared/ProductSummary.ascx", product) %> 

For those of you who are just getting started with a more recent version of ASP.NET MVC, the RenderPartial method replaces the RenderUserControl method in previous versions.  Anyway, I got this compiler error:

“Cannot implicitly convert type ‘void’ to ‘object’.”

It took me TEN minutes to figure out what was wrong…  The RenderPartal method does not return a string, but void.  So, I should have used the following code fragment:

<% Html.RenderPartial(
    "~/Views/Shared/ProductSummary.ascx", product); %>

So, if you get a similar compiler error and don’t see what is wrong, there are two differences, because the method returns void and not a string, as most HtmlHelper methods do:

  1. You have to use <% instead of <%=
  2. You have to put a ; after the closing ) of the RenderPartial statement

If you found this post helpful, please click below to “Kick” it:

kick it on

Content-Transfer-Encoding System.Net.Mail.MailMessage

March 4, 2009

If you have tried to send an email using the System.Net.Mail.MailMessage class and System.Net.Mail.SmtpClient class, you may have come across the problem described in this blog post.  The recipient may receive a garbled message, in which carriage returns are not recognized and are shown are strange characters instead:

This is a test message.=0D=0ANew line should start here.=0D=0A

However, when opening the mail in Outlook, it may appear fine.  It took me some time to figure out what was happing.  I used the TcpTrace tool for intercepting the actual message being sent.  The message message had the following items in its header:

Content-Transfer-Encoding: quoted-printable

When using the obsolete System.Web.Mail classes, the Content-Transfer-Encoding had been 7bit instead of quoted-printable.  I thought the strange characters might have something to do with the Content-Transfer-Encoding being used, and started playing around with the AlternativeView class to add an alternative view to my message which, hopefully, would display correctly in the recipient’s client.  Unfortunately, adding an AlternativeView leaves the original view in the message as well, and this particular client still tried to read that original view. 

At first, I thought I would have to revert to the old System.Web.Mail client.  I decided to try one more Google search to see if there was a way around this, and found a comment by Stanley Roark, buried on the MSDN Library’s page about the MailMessage class.  This solution worked so well for me that I decided it deserved a blog post in its own right, hoping it would make it easier for people to find this solution.  My version of the code, in which you will have to replace some of the strings with those appropriate for your own message:

MailMessage message = new MailMessage(
    "", "");
message.Subject = "Some Subject";
message.IsBodyHtml = false;
message.Body = null;
using (AlternateView body =
        "Some Message Body",
        message.IsBodyHtml ? "text/html" : null))
    body.TransferEncoding = 
        SmtpClient smtp = new SmtpClient("", 25);
    catch (SmtpException ex)

If you found this post helpful, please click below to “Kick” it:

kick it on