IIS7 Hijacking my custom error pages

by Tim 26. January 2010 18:34

Custom error pages replaced by IIS?

Development Machine W7, Production IIS7 Windows 2008

Just had a sticky problem crop up in production. Custom error pages that used to work no longer work, I don’t know when but sometime our custom ASP.NET error pages have been replaced by stock IIS error pages.

How we use custom error pages

If a website user or search bot attempts to access a product page and the product no longer exists, we send them to a custom error page. The page suggests they try another product or search for alternatives. This page sends back a http 404 status so that the search engines know to drop the accessed URL from the index. It also causes our Google Mini to drop the page from its database too. If the user navigates to another url that has never had content they get a default 404 page returned.

Lets have a look

So you have a great little custom error page like this;

Protected Sub Page_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Ensure a 404 not found sent for SEO purposes
        Response.StatusCode = 404
        Response.TrySkipIisCustomErrors = True
 
    End Sub

However even browsing direct to the page you only get,

IIS7 404 default error page

This is not your crafted custom page, instead this is the IIS7 page kicking in. IIS has put itself in the pipeline to the end user, replacing its own page with in place of the custom page.

IIS custom page configuration

The configuration for this is found in the site Error Pages section;

IIS7 Management Link to Error Pages

If you go into this menu you see all the custom error pages set up in IIS, now select the edit feature settings link on the right hand side;

IIS7 Error Pages Configuration

This is where the behaviour is set and currently this is set to show detail errors for users local to the machine but custom errors for people elsewhere. Note this is not ASP.NET, although the idea is the same, this is above ASP.NET, it is the handling IIS does for these pages.

You do not want normal users to see the detailed errors as it can give away important information about the configuration of your server thus the setting shown is fine and looking at it one would guess it would show our custom pages as we saw by the returned IIS 404 page this is not the case.

IIS7 Custom Errors Settings


So why do we not see our custom pages? Well back in the page code behind shot earlier is the key, you must set

Response.TrySkipIisCustomErrors = True

This tells IIS to get its nose out of what we’re up to and let us get on with it. Trying to browse the custom error page, now we get our custom error page retuned as would be expected in the first case.

Lesson learnt

The real nasty bit of this is that our development machines do not exhibit this behaviour, everything looks fine there. As Rick Strahl says it is another good case for using the staging servers with the production environment so that you test everything.

Other links:

Rick Strahl's Web Log has an almost identical post here

What to expect from IIS7 custom error modulethis link was the one that taught me what I needed to solve the problem.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: , ,

.NET | ASP.NET

Gracefully dealing with eConnect errors

by Tim 2. January 2010 22:10

If you need to integrate with Microsoft Dynamics GP one of the options you may choose is to use the eConnect product. eConnect is an API that allows you to submit XML documents to Dynamics GP to perform CRUD operations on most of the document types in GP. Using .NET for integrations, if any issues/problems arise from submissions to eConnect,  via eConnect_EntryPoint or eConnect_Requester methods, then these errors are surfaced as eConnect exceptions. The .Message property of this class contains the error text.

The econnectException class returns the message from the eConnect stored procedure that originated the problem. A table of these messages is held in SQL server, DYNAMICS database, table taErrorCode. This table gives an idea of the error conditions you may not have thought possible and lets you be a bit more proactive at handling errors.

ta_ErrorCode To see the contents of this table see this file:

Item does not allow back orders

Lets choose an example problem that you may experience. When submitting a SOP sales order document, normally you want any items that are out of stock to be back ordered. This is easy, set the QtyShrtOpt = 4 in the XML to back order the balance. However if you have a situation where you have a telesales team hammering in orders as well as a website taking orders, even with SQL replication you can occasionally get a scenario where a web order comes in for an item that has been set to disallow web orders and it no longer has enough stock to satisfy the web order. This may be due to latency in updating stock on the website for example. eConnect lets us know with the following exception: “Microsoft.GreatPlains.eConnect.eConnectException: Error Number = 4776”

Gracefully dealing with it

There a few ways I can think of to deal with this, the chosen one is to change the QtyShrtOpt = 6 for the line in question inorder to cancel the qty that can not be allocated from stock. It is wise to then set an order note to let the sales staff who will process the order know about the issue so it can be resolved with the customer, perhaps alternative item offered.

.NET code

A regular expression is used to parse the eConnect exception text. This allows easy detection of what error has occurred and extracts the item number for the order line raising the exception.

  1. Dim ItemNumberList As New List(Of String)
  2. 'Get the item numbers that exhibit this error
  3. For Each CurrentMatch As RegularExpressions.Match In _
  4.     RegularExpressions.Regex.Matches(ErrorText, _
  5.         "(<taSopLineIvcInsert>.*<ITEMNMBR>(.*)</ITEMNMBR>.*</taSopLineIvcInsert>) --->.*Error Number = 4776", _
  6.         RegularExpressions.RegexOptions.Singleline)
  7.     If CurrentMatch.Groups.Count > 1 Then
  8.         'capture 2 has the itemnmbr
  9.         ItemNumberList.Add(CurrentMatch.Groups(2).Value.Trim)
  10.     End If
  11. Next CurrentMatch

Having a list of item numbers with this issue allows us to change the XML of the document being submitted to alter the quantity shortage option flag to cancel the balance (option 6). Promoting LINQ for XML work, load the XML document into a XDocument (LINQ XML Document) class.

  1. Dim salesdoc As XDocument = XDocument.Parse(xmlSalesOrder.OuterXml)

Now use a LINQ query to get all the elements that need the backorder option changing and change it to 6.

  1. For Each CurrentItem In ItemNumberList
  2.     Dim CurrenItemVar As String = CurrentItem
  3.     For Each CurrentElement As XElement In From salesline In salesdoc.Elements.Descendants("taSopLineIvcInsert") _
  4.                     Where salesline.Element("ITEMNMBR").Value.StartsWith(CurrenItemVar) _
  5.                     Select salesline
  6.         CurrentElement.SetElementValue("QtyShrtOpt", "6")
  7.     Next
  8.     salesdoc.Elements.Descendants("taSopHdrIvcInsert")(0) _
  9.       .SetElementValue("NOTETEXT", _
  10.         salesdoc.Elements.Descendants("taSopHdrIvcInsert")(0).Element("NOTETEXT").Value & vbCr & String.Format( _
  11.         "Item: {0} could not be fully ordered due to no back order allowed flag set on item and lack of stock.", CurrenItemVar))
  12. Next
For each item fixed, the XML of the document we are submitting has order notes appended to take account of the fact there is an issue with this item. The sales are already always reading the order notes for customer comments coming through from the website so should catch these notes.

Now the altered XML document is resubmitted to Dynamics GP via eConnnect. If it fails this time there is an issue that we have not programmed for so it needs administrative intervention.

To load the xDocument back into the XMLDocument class for submission to eConnect,

  1. xmlSalesOrder.Load(salesdoc.CreateReader())

Note on security

Beware exposing your ERP system to your website – if the website gets compromised, then so is your business. eConnect allows most of your business data to be altered and queried - this is something to be very careful of. With the implementation I created, the XML document is punched through the firewalls to a custom web service on the GP segment of the network. This web service only lets through the specific eConnect documents we want to allow through and only those that meet specific criteria to limit the attack potential.

Summary

By adding to this example, common errors your eConnect integration encounters could be eliminated so IT staff are prevented from spending time supporting disruptive day to day integration issues. Thus these problem cases are handed back to the process owner, be that; buyers, warehouse, or sales staff.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: ,

LINQ | .NET | Microsoft Dynamics GP

Scott Guthrie - Manchester

by Tim 2. October 2009 13:09

Agenda

I am so glad I made the effort to go down to Manchester for the day to be inspired by Scott in a really well paced session on ASP.NET MVC – VS2010 / ASP.NET 4.0 goodness  and finally a flash through Silverlight 3.

 

ScotGu 

A few things came out of this day.

  • Like other engineering professions, we have too few women.
  • Developers smell better as the years go by.
  • .NET is exciting the future is bright.
  • Scott is very smart.
  • The Spark programme is really getting developers thinking about starting software houses. Lots of buzz around this new imitative.
  • General observation by many of how many iPhones are at a MS focused event.
  • Manchester has changed a lot since I last had a walk around.
  • Running from The Printworks to the Train at Piccadilly is further than you remember when the train leaves in 15 mins.
  • There is so much you can do now, so few hours in a day.
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags:

.NET | ASP.NET

Team Foundation Server Source Control locks

by Tim 2. October 2009 12:27

Locks

Moving lap tops, desktops, operating systems or having a developer move on all cause issues with occasional items checked out to workspaces you have no control over.

There is a good blog post here on how to resolve these issues with the command line and TF.exe. You may remove workspaces or undo checkout locks on source code by using the commands outlined below.

Undoing a checkout that belongs to another user

James Manning's blog

The key command for my own reference is:

TF.exe undo
 /workspace:<WorkspaceName>;<UserName>
 $/<TeamProject>/<FileLocation> 
 /s:http://<YourTFSServer>:8080

Had to use this today after finding a couple of instances of code checked out on my old vista installation but the files had been migrated to my new Windows 7 install. So undid the checkout finished the changes and checked in the new versions.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags:

.NET


Microsoft Certified Solutions Developer
Microsoft Certified Application Developer
Microsoft Certified Technology Specialist

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Dynamic Code Blocks, Tim Wappat