Network Analyst routing
ArcGIS_Routing_CSharp\Default.aspx.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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
using ESRI.ArcGIS.ADF.Web.Geocode;
using ESRI.ArcGIS.ADF.Web.DataSources;

namespace RouteFinder
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Init(object sender, EventArgs e)
    {
      if (Session.IsNewSession)
      {
        Session.Add("ModifiedNAContextApplied", false);
      }
    }

    /// <summary>
    /// Creates an array of x,y coordinates for the passed locationString.
    /// </summary>
    /// <returns></returns>
    private void GeocodeAddress(IGeocodeFunctionality igf, string address, string zip, out double x, out double y)
    {
      x = Double.NaN;
      y = Double.NaN;

      System.Collections.Generic.List<AddressValue> avc = new System.Collections.Generic.List<AddressValue>();
      AddressValue av1 = new AddressValue("STREET", address);
      AddressValue av2 = new AddressValue("ZONE", zip);
      avc.Add(av1);
      avc.Add(av2);

      ESRI.ArcGIS.ADF.Web.Geometry.Point geocodedPoint = igf.GeocodeAddress(avc);
      x = geocodedPoint.X;
      y = geocodedPoint.Y;

      return;
    }

    /// <summary>
    /// Updates the form elements with values saved in session
    /// </summary>

    private void GetPreviousParameters()
    {
      if (Session["FromAddress"] == null)
        return;

      FromStreet.Value = Session["FromAddress"].ToString();
      FromZip.Value = Session["FromZip"].ToString();
      ToStreet.Value = Session["ToAddress"].ToString();
      ToZip.Value = Session["ToZip"].ToString();
    }

    protected void btnGetDirections_Click(object sender, System.EventArgs e)
    {
      if (!GeocodeResourceManager1.Initialized)
        GeocodeResourceManager1.Initialize();

      IGISResource gisResource = GeocodeResourceManager1.GetResource(0);
      if (gisResource == null)
        throw new Exception("Error getting Geocode resource.  Make sure you have set up the identity in web.config");

      bool supported = gisResource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IGeocodeFunctionality));

      if (supported)
      {
        IGeocodeFunctionality igf = (IGeocodeFunctionality)gisResource.CreateFunctionality(typeof(IGeocodeFunctionality), null);

        double fromX, fromY, toX, toY;

        Session["FromAddress"] = FromStreet.Value.ToString();
        Session["FromZip"] = FromZip.Value.ToString();
        Session["ToAddress"] = ToStreet.Value.ToString();
        Session["ToZip"] = ToZip.Value.ToString();

        //geocode addresses
        GeocodeAddress(igf, FromStreet.Value.ToString(), FromZip.Value.ToString(), out fromX, out fromY);
        GeocodeAddress(igf, ToStreet.Value.ToString(), ToZip.Value.ToString(), out toX, out toY);


        if ((double.IsNaN(toX)) || (double.IsNaN(fromX)))
        {
          AddressErrorText.Visible = true;
          if (double.IsNaN(fromX)) StartingErrorText.Visible = true;
          if (double.IsNaN(toX)) EndingErrorText.Visible = true;
          GetPreviousParameters();
          return;
        }
        //pass points to directions form
        string url = String.Format("Directions.aspx?FromX={0}&FromY={1}&ToX={2}&ToY={3}", fromX, fromY, toX, toY);
        Response.Redirect(url, true);
      }
    }

    protected void Addresses_PreRender(object sender, EventArgs e)
    {
      GetPreviousParameters();
    }

    private string CapitalizeFirstLetters(string inString)
    {
      string[] words = inString.Split(Char.Parse(" "));
      System.Text.StringBuilder sb = new System.Text.StringBuilder();
      string s, s2;
      for (int i = 0; i < words.Length; i++)
      {
        s = words[i].Substring(0, 1);
        if (words[i].Length > 1)
          s2 = words[i].Substring(1);
        else
          s2 = "";
        if (i != 0) sb.Append(" ");
        sb.Append(s.ToUpper() + s2);
      }
      return sb.ToString();
    }


  }
}