Today, I spent some time trying to debug an error I was getting with some of my ASP .Net 2.0 pages.
All of sudden IIS wouldn’t load my pages saying that they did not extend System.Web.UI.Page, when on inspection of the codebehind file I was sure they did.
The problem was that I ran a Resharper 2.0 – Reformat Code (ctrl-alt-f) on my web application with the shorten references item checked. This changed the way in which the page directive worked on the aspx page.
Following is an example that illustrates what the file was originally defined as and what the reformat did to it to cause the application to blow up.
Original syntax
<%@ Page language="c#"
Codebehind="default.aspx.cs"
AutoEventWireup="True"
Inherits="Com.CompanyName.Default"
EnableSessionState="False"
EnableViewState="False"%>
Don't use reformat code with shorten references checked.
Resharper Reformat Code Syntax
<%@ Page language="c#"
Codebehind="default.aspx.cs"
AutoEventWireup="True"
Inherits="Default"
EnableSessionState="False"
EnableViewState="False"%>
<%@ Import namespace="System.Web.UI.WebControls"%>
<%@ Import namespace="Com.CompanyName"%>
Don't use reformat code with shorten references checked.
The reformatted syntax takes the namespace of the Inherits attribute in the Page directive shortening it to “Default” from “Com.CompanyName.Default”. To do this, it has imported the “Com.CompanyName” namespace into the aspx page.
Now because .Net 2.0 defaults non-namespaced elements to the global namespace, the designer file, no longer belongs to the Com.CompanyName namespace however the codebehind page still does.
This causes the conflict throwing the error.
A trap for young players.