如何为 ASP.NET WebAPI 确认电子邮件编码 URL 中的字符?
How can I encode characters in a URL for an ASP.NET WebAPI confirm email?
我有一个 ASP.NET WebAPI 应用程序,需要为确认电子邮件创建一个 link。这是它的代码:
callbackUrl1 = "http://localhost:2757/index.html" +
"?load=email" +
"&userId=" + user.Id +
"&code=" + code;
await UserManager.SendEmailAsync(
user.Id,
"Confirm your account",
"Please click this link: <a href=\"" + callbackUrl1 + "\">link</a>");
我的代码几乎可以正常工作,但似乎我需要对进入查询字符串的数据进行编码。
对于 MVC,这是这样完成的:
var callbackUrl = Url.Action("ConfirmEmail",
"Account",
new { userId = user.Id, code = code },
protocol: Request.Url.Scheme);
但是我没有使用 MVC,当我尝试添加它时,我遇到了一些不相关的问题。
有人能告诉我如何编码(如果这是正确的词)构成 link 的数据吗?
使用HttpUtility.UrlEncode方法。请注意,您仍然需要 HTML 为电子邮件
的 HTML 编码 URL
使用 System.Uri Fiddle here
对 URL 进行编码
using System;
public class Program
{
public static void Main()
{
var callbackUrl1 = new System.Uri("http://localhost:2757/index.html" +
"?load=email" +
"&userId=" + "12345££&^" +
"&code=" + "myCo de");
var aHref = string.Format("<a href=\'{0}'>link</a>", callbackUrl1.AbsoluteUri);
Console.WriteLine(aHref);
}
}
我有一个 ASP.NET WebAPI 应用程序,需要为确认电子邮件创建一个 link。这是它的代码:
callbackUrl1 = "http://localhost:2757/index.html" +
"?load=email" +
"&userId=" + user.Id +
"&code=" + code;
await UserManager.SendEmailAsync(
user.Id,
"Confirm your account",
"Please click this link: <a href=\"" + callbackUrl1 + "\">link</a>");
我的代码几乎可以正常工作,但似乎我需要对进入查询字符串的数据进行编码。
对于 MVC,这是这样完成的:
var callbackUrl = Url.Action("ConfirmEmail",
"Account",
new { userId = user.Id, code = code },
protocol: Request.Url.Scheme);
但是我没有使用 MVC,当我尝试添加它时,我遇到了一些不相关的问题。
有人能告诉我如何编码(如果这是正确的词)构成 link 的数据吗?
使用HttpUtility.UrlEncode方法。请注意,您仍然需要 HTML 为电子邮件
的 HTML 编码 URL使用 System.Uri Fiddle here
对 URL 进行编码using System;
public class Program
{
public static void Main()
{
var callbackUrl1 = new System.Uri("http://localhost:2757/index.html" +
"?load=email" +
"&userId=" + "12345££&^" +
"&code=" + "myCo de");
var aHref = string.Format("<a href=\'{0}'>link</a>", callbackUrl1.AbsoluteUri);
Console.WriteLine(aHref);
}
}