HttpParseException - The server tag is not well formed - ASPX error line number

Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!


HttpParseException - The server tag is not well formed - ASPX error line number

Recently I was coming across this error message : The server tag is not well formed. An exception saying that there is something wrong with the ASPX markup on my web page, usually a control isn't correctly typed, maybe it's missing the closing carrot bracket.

My problen was that the line number wasn't been included in the error message, so trying to easily find which part of the ASPX code had the error was a little bit like finding the needle in the haystack, there are other ways, but I really prefer to have a line number, and then with line numbers enabled in Visual Studio, I can get straight to the error and fix it very quickly, without having to worry about searching for where the error is. It's really useful to have a line number.

ADO.NET had been available since .NET 1 and so has been used for many years. More recently we've had releases such as LINQ to SQL and the Entity Framework, which may well replace ADO.NET in many modern applications.

We used a generic error handler, catching exceptions and sending emails to notify of us of any errors, and for whatever reason, this particular error didn't have the line number included in the exception message or details attached. Although

Global.asax - Application_Error

You should really be catching Exceptions where they happen in the code so you can deal with them more specifically, and you shouldn't be relying on a global catch all exceptions at the application level, however catching any Exceptions you've missed at the application level can be considered your last line of defence in stopping the exception raise upto the customer and get away from your loggin. (In fact Custom errors make a good last line of defence.)

The following code shows how to catch errors using the Global.asax in the Application_Error event which will capture any unhandled exceptions which have propogated from the page. This is actually quite useful for this type of error, as it will catch this exception whereas your in code exception handling probably won't.

void Application_Error(object sender, EventArgs e)
    // Code that runs when an unhandled error occurs
    Exception ex = Server.GetLastError();

    // get line number from ASPX parse error
    System.Web.HttpParseException httpParseEx = ex as System.Web.HttpParseException;

    if (httpParseEx != null)
        String lineNumber = "Line number: " + httpParseEx.Line;


In the Application_Error event in the Global.asax you can use Server.GetLastError() method to get a reference to the exception which has occurred.

HttpParseException - Line Number

We use the 'as' keyword to try to convert the Exception to a HttpParseException, without causing a new Exception should the conversion fail. If the conversion succeeds, we can then get the line number of error the the .Line property of the HttpParseException. This is the line number in the ASPX page where the 'The server tag is not well formed' error has oocurred, allowing you to get the error straight away and make your correction without wasting much time.

Example of broken ASPX code

Here is an ASP.NET Label control which isn't correctly formed (it's missing the closing carrot bracket : >) and will produce the error : The server tag is not well formed

<asp:Label ID="Label_SomeLabel" runat="server" Enabled="true" Text="Hello there"