您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ApiDescriptionExtensions.cs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Text;
  3. using System.Web;
  4. using System.Web.Http.Description;
  5. namespace CacheControl_test.Areas.HelpPage
  6. {
  7. public static class ApiDescriptionExtensions
  8. {
  9. /// <summary>
  10. /// Generates an URI-friendly ID for the <see cref="ApiDescription"/>. E.g. "Get-Values-id_name" instead of "GetValues/{id}?name={name}"
  11. /// </summary>
  12. /// <param name="description">The <see cref="ApiDescription"/>.</param>
  13. /// <returns>The ID as a string.</returns>
  14. public static string GetFriendlyId(this ApiDescription description)
  15. {
  16. string path = description.RelativePath;
  17. string[] urlParts = path.Split('?');
  18. string localPath = urlParts[0];
  19. string queryKeyString = null;
  20. if (urlParts.Length > 1)
  21. {
  22. string query = urlParts[1];
  23. string[] queryKeys = HttpUtility.ParseQueryString(query).AllKeys;
  24. queryKeyString = String.Join("_", queryKeys);
  25. }
  26. StringBuilder friendlyPath = new StringBuilder();
  27. friendlyPath.AppendFormat("{0}-{1}",
  28. description.HttpMethod.Method,
  29. localPath.Replace("/", "-").Replace("{", String.Empty).Replace("}", String.Empty));
  30. if (queryKeyString != null)
  31. {
  32. friendlyPath.AppendFormat("_{0}", queryKeyString.Replace('.', '-'));
  33. }
  34. return friendlyPath.ToString();
  35. }
  36. }
  37. }