using System;
using System.Text;
using System.Web;
using System.Web.Http.Description;
namespace Service.MeterProcessState.Areas.HelpPage
{
public static class ApiDescriptionExtensions
{
///
/// Generates an URI-friendly ID for the . E.g. "Get-Values-id_name" instead of "GetValues/{id}?name={name}"
///
/// The .
/// The ID as a string.
public static String GetFriendlyId(this ApiDescription description)
{
String path = description.RelativePath;
String[] urlParts = path.Split('?');
String localPath = urlParts[0];
String queryKeyString = null;
if (urlParts.Length > 1)
{
String query = urlParts[1];
String[] queryKeys = HttpUtility.ParseQueryString(query).AllKeys;
queryKeyString = string.Join("_", queryKeys);
}
StringBuilder friendlyPath = new StringBuilder();
friendlyPath.AppendFormat("{0}-{1}",
description.HttpMethod.Method,
localPath.Replace("/", "-").Replace("{", string.Empty).Replace("}", string.Empty));
if (queryKeyString != null)
{
friendlyPath.AppendFormat("_{0}", queryKeyString.Replace('.', '-'));
}
return friendlyPath.ToString();
}
}
}