Home > .NET, Vs2010 > IIS 7.5 difference between Windows 7 and Server 2008 r2

IIS 7.5 difference between Windows 7 and Server 2008 r2

2012/07/10

I think that is not a bad idea to develop directly with a Windows server 2008 R2 as development pc OS, because sometimes you can encounter subtle differences as the one explained in this post.

I’m developing with a HP Notebook (HP quality is a bit degraded from the glorious ’80: my screen after 2 years has some very little scratches caused from the keyboard keys that touches the screen when the notebook is closed …) , with Windows 7 ultimate 64 bit as OS.

IIS is the latest release:


I’m working to an ASP.NET 4 project , where i’m heavily using JQuery, so i have a lot of page methods (no postback! is evil !) called by passing JSON strings , the page method does something and returns to a javascript callback.

Tipically in a db (in this case SQL Server 2012) there a a lot of constraints and checks, that causes an error for example in a update record method.

The catch code in my page methods is always something as (but there are many other magic numbers…):

catch (SqlException ex)
{
    switch (ex.Number)
    {
        case 577:
        case 547:
        case 2627:
        case 2601:
            //Violation of PRIMARY KEY constraints
            objDB = new DbViolations();
            if (objDB.ManagePKFKConstraintsMsgs("MyDbTable", ex.Message) == true)
            {
                ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), objDB.MsgToUser);
                Response.StatusCode = (int)HttpStatusCode.Conflict;
            }
            else
            {
                // case already managed, not useful that the user knows the details
                ... Some logging
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
            }
            break;

There is a routine (ManagePKFKConstraintsMsgs) that verify if the error string contains the name of a check (for example) , if so the function returns a custom string for the user , that tipically is showed in a window.alert; otherwise the better thing to do is a error log and then redirect the user to a custom “Yellow Death Page”.
In my example objDB.MsgToUser can returns something as “The age must be between 0 and 99”, the javascript callback is, for example

function cmdSave_onComplete(xData, status) {
    if (status == "success") {
        $("#frmEditAddCateg").dialog("close");
        $("div#divGridViewCateg").html(xData.responseText);
    } else {
        if (xData.status == 409) {
            //HttpStatusCode.Conflict , was provided a injection of a alert to the user
            $("div#divGridViewCateg").html(xData.responseText);
            return;
        }
        window.location = "../Users/GenericErrorPage.aspx";
    }
}

Note that i’m returning from C# page method code a Response.StatusCode = (int)HttpStatusCode.Conflict , which has value = 409

So in the callback i test the status : if 409 then the responseText is my custom message , to be showed to user with a window.alert which is included in the xData.responseText , among other markup.

On my development pc all ok (this is the first time i have tried this new approach), instead when i have published the site on the production Windows Server 2008 r2 , surprise : in xData.responseText there was the string “The page was not displayed because there was a conflict”, so this was the result:


I have verifyed the Windows Server 2008 r2 IIS version:


Ops… same version of my Win 7… so ?

The answer is that IIS in Windows 2008 R2 overrides by default the HTTP response with its default error management (pages and other) , this default behavior can be fortunately overriden by using the TrySkipIisCustomErrors property of the Response object.

So my C# code now is:

catch (SqlException ex)
{
    switch (ex.Number)
    {
        case 577:
        case 547:
        case 2627:
        case 2601:
            //Violation of PRIMARY KEY constraints
            objDB = new DbViolations();
            Response.TrySkipIisCustomErrors = true;   //  <------------------- new line
            if (objDB.ManagePKFKConstraintsMsgs("MyDbTable", ex.Message) == true)
            {
                ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), objDB.MsgToUser);
                Response.StatusCode = (int)HttpStatusCode.Conflict;
            }
            else
            {
                // case already managed, not useful that the user knows the details
                ... Some logging
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
            }
            break;

And using the sitehosted in Windows Server the result is as expected:


Unfortunately, there are problems also between different versions of Windows server: for example some years ago i had serious problems with date fields with Latvian as browser language ( a website used also from an hospital in Riga) with the pages hosted in a Windows 2003 server; the hosting migrated to Windows 2008 and et voilà , problem disappeared.

Advertisements
Categories: .NET, Vs2010
%d bloggers like this: