April 28, 2024

ASP.NET 2.0 Namespace Issues

A nifty feature of ASP.NET 2.0 is the ability to store all of your application’s code in the App_Code folder and automatically have it be compiled into one big, happy namespace.  Most of the time this works well; however, when you’re doing a forms authentication-based application and are:  A) too lazy to deal with Membership; or B) bound to another way of doing things, this can bite you.
What is the natural name you’d give to a page used to log in to an application?  Login.aspx, right?
Unfortunately you cannot use this name because the Login class name is already consumed in the default namespace.  Even more unfortunately this will work wonderfully until you compile and publish your application.  At that point you will receive this error:

Server Error in ‘/YourApp’ Application.


Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0030: Cannot convert type ‘ASP.login_aspx’ to ‘System.Web.UI.WebControls.Login’
Source Error:

Line 112:        public login_aspx() {
Line 113: string[] dependencies;
Line 114: ((Login)(this)).AppRelativeVirtualPath = "~/Login.aspx";
Line 115: if ((global::ASP.login_aspx.@__initialized == false)) {
Line 116: dependencies = new string[1];

Correcting this problem is not difficult – simply rename your .aspx file and change the code-behind class’s name.

Deducing that this is the underlying problem, however, can be a real pain!

Leave a Reply