Last active
December 10, 2015 06:08
-
-
Save JeffJacobson/4392702 to your computer and use it in GitHub Desktop.
Demonstrates how to detect a WSDOT Traveler API REST Request URL and append an access code from web.config. Designed for use with a Proxy page, as described at http://help.arcgis.com/en/webapi/javascript/arcgis/help/jshelp_start.htm#jshelp/ags_proxy.htm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Appends an AccessCode parameter to the input URI if it is a WSDOT Traveler Info REST API URI and does not already have an AccessCode parameter. | |
/// </summary> | |
/// <param name="uri">The URI passed to the proxy. Note that this string will be modified if an access code is determined to be necessary.</param> | |
/// <example> | |
/// <code><![CDATA[public void ProcessRequest (HttpContext context) { | |
/// HttpResponse response = context.Response; | |
/// // Get the URL requested by the client (take the entire querystring at once to handle the case of the URL itself containing querystring parameters) | |
/// string uri = context.Request.Url.Query.Substring(1); | |
/// AddTravelerApiAccessCodeIfNecessary(ref uri); | |
/// // Do the rest of your stuff... | |
/// } | |
/// ]]></code> | |
/// </example> | |
private static void AddTravelerApiAccessCodeIfNecessary(ref string uri) | |
{ | |
const string urlPattern = @"(?i)wsdot\.wa\.gov\/Traffic\/api\/(\w+)\/\1REST\.svc"; | |
const string accessCodePattern = @"AccessCode=([^\s&])+"; | |
if (Regex.IsMatch(uri, urlPattern)) // If this is a traveler API URL... | |
{ | |
if (!Regex.IsMatch(uri, accessCodePattern, RegexOptions.None)) // If the URL does not already have an AccessCode parameter... | |
{ | |
string code = ConfigurationManager.AppSettings["wsdotTrafficApiAccessCode"]; | |
char separator = uri.Contains("?") ? '&' : '?'; | |
uri = string.Format("{0}{1}AccessCode={2}", uri, separator, code); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment