I have the strangest problem! I have an ASP.NET website application (luckily not yet live). Yesterday and everyday before, I could run the application without error. It was working fine when I turned the computer off late last night. When I ran the application today, I got a strange message telling me that my website was offline and that I should remove the app_offline.htm
page from my root directory to put it back online.
I have never seen this message or page before, so I searched online to find that apparently SQL Server can put it there temporarily while it's busy to stop any more incoming requests (or something similar). Its proper use is to stop requests going to your site when you are updrading or doing maintenance. I proceeded to remove it as suggested. I cleaned and built the solution and then tried to run it. It no longer runs.
Now, I get an error in my MasterPage
code behind where I normally access the controls on the MasterPage
in the Page_Load
event handler (shown below).
protected void Page_Load(object sender, EventArgs e)
{
breadCrumb.BreadCrumbs = BreadCrumbs;
header.MenuItems = MenuItems;
footer.Footers = Footers;
head.Controls.Add(CreateCssLink("~/Stylesheets/main.css", "screen"));
head.Controls.Add(CreateJavaScriptLink("~/Scripts/site.js"));
if (Context.Session != null && Session["layout"] == null) Session["layout"] = "single";
if (!ArePreferencesVisible) preferences.Visible = false;
}
I want to be clear here... there are no errors in this code. There are correctly named elements in the MasterPage
that correspond to those in this code. It has always worked as expected up until today and I have not changed any code since it was last working. I now get a NullReferenceException
on the first line because the breadCrumb
element is null
. In fact, all the elements from the page that are referenced here are null
.
Normally the page elements have been initialised by the time the Page_Load
event is raised. What can have caused this to stop happening?
I have double checked that the MasterPage and its code behind file are still connected and they are. I also tried moving this code to the Page_LoadComplete
event handler, but the controls had still not been initialised, even at that stage. I shouldn't really have to change the code because it worked just fine before.
I also commented out this code and the website ran, albeit without any styles. Interestingly, when I returned the original code to the event handler and refreshed the page, it actually worked again just as it used to. I loaded a number of different pages successfully and I thought it had fixed itself, so I restarted the application and then got the same error again. No amount of cleaning, building, changing solution configuration or restarting the application and Visual Studio make any difference.
I'm not sure if the app_offline.htm
is a red herring, but I've never seen that before either so it might be connected somehow. From what I read, it seems that just removing it causes the server to restart the application domain automatically, so I can't see the connection.
I really hope someone has come across this bizarre situation before because I'm all out of ideas and I can't run my website until this is fixed.
Ok, so I finally tracked down the problem. I had an earlier backup of the website from a few days ago, so I loaded that up and it worked, albeit missing the last few days' work. I copied the latest project code into the Visual Studio > Projects folder leaving the old code in the Visual Studio > Website folder. I loaded the solution and it ran fine again.
I basically compared the Website files, updating each one in turn and periodically running the application. Eventually, the application stopped working. The last file that I had updated was a MasterPage
file that extended the MasterPage
that had the problem.
What I had done was comment out an empty Page_Load
event handler in the MasterPage
. I don't understand why that would cause this problem, so any comments explaining that would be helpful and appreciated.
This MasterPage
is only used on the home page and a few others, so that explains why I didn't come across the problem the night that I changed it. (I hadn't viewed any pages that used that MasterPage
after making the change). I wish I had because it would have been much easier to track down then, but such is life.
So I replaced all of my latest code, added the empty Page_Load
event handler in the MasterPage
again and finally, everything is working again.