In the new asp.net 2.0 web application model, every web page (.aspx) and it’s code behind (the class that this page “Inherits” from, actually it’s partial with it, but that’s the property name) compiles into a single assembly. At least as a default behavior.
That means that the following relationship is completely valid in this compilation model:
Default.aspx (CodeFile=Default.aspx.cs, Inherits=MyNamesapce.MyDefaultPage)
Default.aspx.cs (class name MyNamespace.MyDefaultPage)
Default2.aspx (CodeFile=Default2.aspx.cs, Inherits=MyNamesapce.MyDefaultPage) // Inherits: same as above
Default2.aspx.cs (class name MyNamespace.MyDefaultPage) // class name: same as above
This can be quite confusing though. While you’re developing everything will work as expected as every .aspx & aspx.cs will compile to a different assembly so no collision will occur; So you think you’re good to go…
The problems start to show up when you’re trying to compile everything to a single assembly (production env.). Now you’ll get an “aspnet_merge.exe” error which tells you that you can’t combine (1) Default.aspx with (2) Default2.aspx and (3) MyNamespace.MyDefaultPage class. It makes sense, after all, those three are partial classes.
When can this happen ?
if you Copy-Paste an existing page & forget to change the name of the class in the CodeBehind and in the Inherits property.
Solution – How to check if you’re really good to go:
I’ve written a small utility with Mario, my teammate, which will find this duplication and give you a little report telling ya if you have more than 1 aspx file which inherit from the same class. Running it on the example above will produce the following report:
Now all I have to do is change the Default.aspx.cs class name and change the Inherits property name in Default.aspx.
How to use the “Duplication Inspector” utility ?
(1) Extract the zip file (down below).
(2) Open app.config and change the “WebDirectoryToInspect” as needed.
(3) Run the InheritsDuplicationInspector.exe and voila !
You can find the source files here:
InheritsDuplicationInspector.zip – 47 KB