Network Analyst routing
ArcGIS_Routing_CSharp\App_Code\CustomTools.cs
// Copyright 2011 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 ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.DataSources;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Web;
using ESRI.ArcGIS.ADF.Web.Geometry;
using ESRI.ArcGIS.Server.Web.NetworkAnalyst;
using System;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
using ESRI.ArcGIS.ADF.Web;

namespace RouteFinder
{
  public class CustomTools : IMapServerCommandAction, IMapServerToolAction
  {
    public void ServerAction(ToolbarItemInfo info)
    {
      ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)info.BuddyControls[0];

      switch (info.Name)
      {
        case "FullRoute":
          Envelope fullextent = mapctrl.Page.Session["PathExtent"] as Envelope;
          if (fullextent != null)
          {
            mapctrl.Extent = fullextent;
          }
          break;
        case "ZoomToStep":
          HtmlInputHidden Step = (HtmlInputHidden)mapctrl.Page.FindControl("Step");
          int step = Convert.ToInt32(Step.Value);
          if (step < 1) step = 1;
          Envelope[] extents = mapctrl.Page.Session["DirectionExtents"] as Envelope[];
          if (extents != null)
          {
            Envelope extent = extents[step];
            mapctrl.Extent = extent;
          }
          break;
        case "ReverseDirections":
          IBaseRouteFinderPage bpi = (IBaseRouteFinderPage)mapctrl.Page;

          // get coords and locations from session
          double toX = Convert.ToDouble(mapctrl.Page.Session["FromX"]);
          double toY = Convert.ToDouble(mapctrl.Page.Session["FromY"]);
          double fromX = Convert.ToDouble(mapctrl.Page.Session["ToX"]);
          double fromY = Convert.ToDouble(mapctrl.Page.Session["ToY"]);
          string fromAddress = Convert.ToString(mapctrl.Page.Session["ToAddress"]);
          string toAddress = Convert.ToString(mapctrl.Page.Session["FromAddress"]);
          // solve the route and display

          if (fromX == 0.0 && toX == 0.0 && fromY == 0.0 && toY == 0.0)
            return;

          NetworkAnalystRouteResult result = bpi.SolveRoute(fromX, fromY, toX, toY, fromAddress, toAddress);
          bpi.DisplayDirections(result, fromAddress, toAddress);
          mapctrl.Page.Session["FromX"] = fromX;
          mapctrl.Page.Session["FromY"] = fromY;
          mapctrl.Page.Session["ToX"] = toX;
          mapctrl.Page.Session["ToY"] = toY;
          mapctrl.Page.Session["FromAddress"] = fromAddress;
          mapctrl.Page.Session["ToAddress"] = toAddress;

          break;
        case "NewDirections":
          string url = "Default.aspx?Reset=true";
          mapctrl.Page.Response.Redirect(url, true);
          break;
        case "PrintVersion":
          MapFunctionality mf = (MapFunctionality)mapctrl.GetFunctionality(0);
          mapctrl.ImageFormat = ESRI.ArcGIS.ADF.Web.WebImageFormat.PNG8;
          ESRI.ArcGIS.ADF.Web.MapImage mi = mf.DrawExtent(mapctrl.Extent);

          MimeData md = mi.MimeData;
          mapctrl.Page.Session["mymimedata"] = md;

          mapctrl.Page.Response.Write("<script>window.open('" + "PrintPage.aspx', 'PrintWindow', 'dependent=yes ,width=800, height=500, status=no, toolbar=no, menubar=yes, location=no, resizable=yes, scrollbars=yes'); </script>");

          break;
      }

    }

    #region IMapServerToolAction Members

    public void ServerAction(ToolEventArgs args)
    {
      throw new System.Exception("The method or operation is not implemented.");
    }

    #endregion
  }
}