Skip navigation.

Be Careful With Cached User ControlsAll recent postsBook Review: Applied .NET Attributes

Default Namespace Setting Breaks Embedded Resources

There’s this little obscure setting in Project Properties that has gotten me in trouble several times. I tend to forget about it until it bites me in the butt.

Suppose you have a web project called Zoo. If you want every web form to automatically be placed in the Zoo.Web.UI namespace, you’d go to Project | Zoo properties and type Zoo.Web.UI next to Default Namespace.

Default namespace property

Next, if you want to place all textual content in a resource file and thus save yourself from hair pulling when it comes to shipping your web app in multiple foreign languages, you add a resource file called Strings.resx, for example. Samples of localization code that I’ve seen normally rely on the fact that, given a project named Zoo, the resource file will be embedded into the assembly as String.sesources.

The weird thing is with the default namespace of Zoo.Web.UI the compiler prepends it to the embedded resources, producing Zoo.Web.UI.Strings.resources!

The Default Namespace is used to fill out class templates when you add regular classes to the project. All you get out of it is a namespace declaration:

namespace Zoo.Web.UI
{
 …
}

If you don’t want this namespace you can change it by hand. But resource files are stuck with the default namespace setting! I haven’t found a way to change it yet. Many times I’ve received a run-time error stating that my resources were embedded incorrectly… because of the implicit default namespace prepended to the resource collection.

Even though a default namespace seems handy, I make sure I clear it out every time I start a new project. Just to be on the safe side.

Comments

No comments yet

Emails and Notifications

Would you like to be notified when somebody responds to this post? 

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):