在单独的类中获取 C# Request.Form
本文关键字:Request Form 获取 单独 | 更新日期: 2023-09-27 17:56:12
我正在编写一个C#
助手来解析HTML
POST
并创建一个有符号字符串。 该字符串用于辅助形式的隐藏输入。 如何在静态C#
方法中访问Request.Form
字段? HttpWebResponse
?使用System.Web.UI.Page
?
public static string cyberSourceCheckoutHtml()
{
List<string> _string = new List<string>();
IDictionary<string, string> parameters = new Dictionary<string, string>();
try
{
if (Request.Form.AllKeys.Length > 0)
{
foreach (var key in form.AllKeys)
{
_string.Add("<input type='"hidden'" id='"" + key + "'" name='"" + key + "'" value='"" + Request.Params[key] + "'"/>'n");
parameters.Add(key, Request.Params[key]);
}
}
string sig = Corbis.Web.UI.CheckoutV2.Helpers.CybersourceSecureAcceptance.sign(parameters);
_string.Add("<input type='"hidden'" id='"signature'" name='"signature'" value='"" + sig + "'"/>'n");
//<form action="https://testsecureacceptance.cybersource.com/silent/pay" method="post"/>
return _string.ToString();
}
catch
{
return "";
}
}
您有 2 个选项:
- 将与 Web 相关的数据放入非 Web 相关类中,一个更简洁且首选的选项是将它们作为非 Web 类型传递。
例如,Request.Form 数据只不过是一个 NameValueCollection。 所以你可以有这样的方法
public static string cyberSourceCheckoutHtml(NameValueCollection formData)
{
// use formData instead of Request.Form
}
选项 #2 是使用 System.Web 命名空间中提供的静态帮助程序属性来访问请求。
NameValueCollection formData = HttpContext.Current.Request.Form;