PageUtil.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Alipay.EasySDK.Kernel.Util
  5. {
  6. /// <summary>
  7. /// 生成页面信息辅助类
  8. /// </summary>
  9. public static class PageUtil
  10. {
  11. /// <summary>
  12. /// 生成表单
  13. /// </summary>
  14. /// <param name="actionUrl">表单提交链接</param>
  15. /// <param name="parameters">表单参数</param>
  16. /// <returns>表单字符串</returns>
  17. public static string BuildForm(string actionUrl, IDictionary<string, string> parameters)
  18. {
  19. return "<form name=\"punchout_form\" method=\"post\" action=\""
  20. + actionUrl
  21. + "\">\n"
  22. + BuildHiddenFields(parameters)
  23. + "<input type=\"submit\" value=\"立即支付\" style=\"display:none\" >\n"
  24. + "</form>\n"
  25. + "<script>document.forms[0].submit();</script>";
  26. }
  27. private static string BuildHiddenFields(IDictionary<string, string> parameters)
  28. {
  29. if (parameters == null || parameters.Count == 0)
  30. {
  31. return "";
  32. }
  33. StringBuilder stringBuilder = new StringBuilder();
  34. foreach (var pair in parameters)
  35. {
  36. if (pair.Key == null || pair.Value == null)
  37. {
  38. continue;
  39. }
  40. stringBuilder.Append(BuildHiddenField(pair.Key, pair.Value));
  41. }
  42. return stringBuilder.ToString();
  43. }
  44. private static string BuildHiddenField(string key, string value)
  45. {
  46. StringBuilder builder = new StringBuilder(64);
  47. builder.Append("<input type=\"hidden\" name=\"");
  48. builder.Append(key);
  49. builder.Append("\" value=\"");
  50. //转义双引号
  51. String a = value.Replace("\"", "&quot;");
  52. builder.Append(a).Append("\">\n");
  53. return builder.ToString();
  54. }
  55. }
  56. }