我喜欢为这种大规模字符串构造首先建立一个数据结构。在这种情况下,一本字典的工作原理:
string CreateUrl(string firstName, string lastName, int supplierID, int login, string payerCountry, string email, decimal amount)
{
int personId = 0;
var query = new Dictionary<string, string>
{
{ "SupplierId", "36" },
{ "RegisteredUserLogin", "T" + login.ToString().PadLeft(5, '0') },
{ "Mode", "RegisteredLoginLess" },
{ "RegisteredModeFunction", "AutoShowTotals" },
{ "PayerCountry", payerCountry },
{ "ForcePayerEmail", email },
// etc ...
{ "FailureCallbackURL", "http://yourfailure.html" },
};
string baseUrl = "https://example.com/PRR/Info/Login.aspx?";
// construct the query string:
// join the key-value pairs with "=" and concatenate them with "&"
// URL-encode the values
string qstring = string.Join("&",
query.Select(kvp =>
string.Format("{0}={1}", kvp.Key, HttpServerUtility.UrlEncode(kvp.Value.ToString()))
)
);
return baseUrl + qstring
}
(注意查询字符串值必须是URL编码,以确保它们不会与预留的网址字符,如“&”冲突)
现在你可以构造URL在您的ASPX页面:
<script runat="server">
public string URL
{
get
{
// TODO insert the user's fields here
return CreateUrl(FirstName, LastName, ...);
}
}
</script>
<a href='<%= URL %>'>Login</a>
另外一个音符 - 这听起来像你想构建新用户自动增量ID。这是使用数据库最容易做到的(数据库可以比Web服务器更容易地处理并发和持久性)。我建议将一个记录插入带有自动增量字段的表格中,并使用数据库生成的值作为ID。
这是一个有趣的网址。 –
“我需要生成一个HTML链接”是什么意思?你的意思是,你需要输入字段并为你的页面生成一个''标签? – McGarnagle
愚蠢的事情,但如果你是一个非技术人员,你可能会考虑改变你的用户名为“notaprogrammer”... – NotMe