Network Analyst routing
ArcGIS_Routing_CSharp\App_Code\CustomMapHandler.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

using System;
using System.Web;

namespace MyHandler
{
    public class CustomMapHandler : ESRI.ArcGIS.ADF.Web.UI.WebControls.MapHandler
    {
        ServerObjectStateModifier serverObjectStateModifier = null;
        ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItem m_resource = null;

        public CustomMapHandler()
            : base()
        {
            serverObjectStateModifier = new ServerObjectStateModifier();
        }

        protected override void OnResourceInit(ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem resource)
        {
            base.OnResourceInit(resource);

            if (resource.Name == "MapResourceItem0")
                CustomResourceInit(resource);
        }
        
        protected void CustomResourceInit(ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItem resource)
        {
            object modifiedNAContextApplied = System.Web.HttpContext.Current.Session["ModifiedNAContextApplied"];
            if (modifiedNAContextApplied == null || !(bool)modifiedNAContextApplied)
            {
                m_resource = resource;

                serverObjectStateModifier.ApplySessionNAContext(resource);
                System.Web.HttpContext.Current.Session["ModifiedNAContextApplied"] = true;
            }
        }

        protected override void OnResourcesDispose(ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager resourceManager)
        {
            base.OnResourcesDispose(resourceManager);

            if ((bool)System.Web.HttpContext.Current.Session["ModifiedNAContextApplied"])
            {
                if (m_resource != null)
                {
                    serverObjectStateModifier.ApplyOriginalNAContext(m_resource);
                    System.Web.HttpContext.Current.Session["ModifiedNAContextApplied"] = false;
                    m_resource = null;
                }
            }
        }
    }
}