[英]Razor Engine: URL generation in template of & generates &
I have an ASP.NET Core application which use Identity for user management. 我有一个ASP.NET核心应用程序,它使用Identity进行用户管理。
I have a MailProvider
which prepares an email to send via smtp for account activation purposes: 我有一个
MailProvider
,它准备一封电子邮件,通过smtp发送以进行帐户激活:
// ...
var activationMailViewModel = new ActivationMailViewModel()
{
LastName = user.LastName,
FirstName= user.FirstName,
UserName = userName,
Email = user.Email,
CompanyName = companyName,
Url = url.Action("ConfirmEmail", "Account", new { Area = "", code = token, userId = user.Id }, request.Scheme)
};
// ...
var result = Engine.Razor.RunCompile(new LoadedTemplateSource($"Activation", path), "SendUserAccountCreation" + Guid.NewGuid(), null, activationMailViewModel);
// ...
var mailMessage = new MailMessage(
new MailAddress("kyc@vente-privee.com", "KYC-vente-privee.com"),
new MailAddress(user.Email, userName))
{
Subject = GetGlobalString("ActivationMail_Subject", cultureCode),
BodyEncoding = System.Text.Encoding.UTF8,
SubjectEncoding = System.Text.Encoding.UTF8
};
<p><a href="@Model.Url" class="button">ACTIVATE MY ACCOUNT</a></p>
However it seems that the link generated can be interpreted in two different ways due to the transcription of the character &
into &
然而,似乎所产生的链接可以以两种不同的方式解释,因为字符的转录
&
&
in the query string of the url. 在url的查询字符串中。
http://base.url.net/Account/ConfirmEmail?code=CfDJ8PtYvJr8Ve1GnxXJykedIzKTQDg%2FTXBwV6NmIYMy8Gi7yUbqZagGbZRacKSFrE717h%2FGjmm6l8QA3knPPgxyNnM1vxe3wb6KnFsGtZUOMTas7QhX1MW5dE4cU5sorA99Dz03zV8ldVMOMP5BGfUrts2nNQqbs8dNLPNgupdkNzaWa4q6fM5u9E99CzRcFjAn7nnd57Ht3IIREAqz6lqufFYo469%2BN2VJxmNJJ1p6OAvO6dMJ9M%2Fzdz3xkpBajJbxRw%3D%3D **&**userId=21e4673c-f121-417f-9837-7f5b234f6f01
http://base.url.net/Account/ConfirmEmail?code=CfDJ8PtYvJr8Ve1GnxXJykedIzKTQDg%2FTXBwV6NmIYMy8Gi7yUbqZagGbZRacKSFrE717h%2FGjmm6l8QA3knPPgxyNnM1vxe3wb6KnFsGtZUOMTas7QhX1MW5dE4cU5sorA99Dz03zV8ldVMOMP5BGfUrts2nNQqbs8dNLPNgupdkNzaWa4q6fM5u9E99CzRcFjAn7nnd57Ht3IIREAqz6lqufFYo469%2BN2VJxmNJJ1p6OAvO6dMJ9M%2Fzdz3xkpBajJbxRw%3D%3D ** **&= userId的21e4673c-f121-417f-9837-7f5b234f6f01
http://base.url.net/Account/ConfirmEmail?code=CfDJ8PtYvJr8Ve1GnxXJykedIzKTQDg%2FTXBwV6NmIYMy8Gi7yUbqZagGbZRacKSFrE717h%2FGjmm6l8QA3knPPgxyNnM1vxe3wb6KnFsGtZUOMTas7QhX1MW5dE4cU5sorA99Dz03zV8ldVMOMP5BGfUrts2nNQqbs8dNLPNgupdkNzaWa4q6fM5u9E99CzRcFjAn7nnd57Ht3IIREAqz6lqufFYo469%2BN2VJxmNJJ1p6OAvO6dMJ9M%2Fzdz3xkpBajJbxRw%3D%3D **&**userId=21e4673c-f121-417f-9837-7f5b234f6f01
http://base.url.net/Account/ConfirmEmail?code=CfDJ8PtYvJr8Ve1GnxXJykedIzKTQDg%2FTXBwV6NmIYMy8Gi7yUbqZagGbZRacKSFrE717h%2FGjmm6l8QA3knPPgxyNnM1vxe3wb6KnFsGtZUOMTas7QhX1MW5dE4cU5sorA99Dz03zV8ldVMOMP5BGfUrts2nNQqbs8dNLPNgupdkNzaWa4q6fM5u9E99CzRcFjAn7nnd57Ht3IIREAqz6lqufFYo469%2BN2VJxmNJJ1p6OAvO6dMJ9M%2Fzdz3xkpBajJbxRw%3D%3D ** **&= userId的21e4673c-f121-417f-9837-7f5b234f6f01
Which can be problematic for the my AccountController
: 这可能对我的
AccountController
有问题:
[Authorize]
public class AccountController : BaseController
{
// GET: /Account/ConfirmEmail
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string code, string userId)
{
if (code == null || userId == null)
{
return View("Error");
}
// Rest of the code... not relevant to the question
}
If the browser / mail client interprets &
as &
如果浏览器/邮件客户端解释
&
作为&
then the userId
will be set to null
and the account / email cannot be confirmed. 然后
userId
将设置为null
并且无法确认帐户/电子邮件。
For example: 例如:
&
in the query string, which is just fine. &
的地址,这很好。 &
&
and hence the link does not confirm the email. However in both email providers, the plain text shows that the url has been generated with the Razor engine is: &
然而,在两个电子邮件提供商中,纯文本显示使用Razor引擎生成的URL是:
&
. 。
What is the best strategy so that my users do not end up with a link that does not work. 什么是最好的策略,以便我的用户不会得到一个不起作用的链接。
Seems the issue was about the raw formatting which was not applied to the email. 似乎问题是关于未应用于电子邮件的原始格式。
The answer given here on SO : 这里给出的答案是:
When doing e-mails, I use the RazorEngineService in RazorEngine.Templating, eg in my case, it looks like this:
在做电子邮件时,我在RazorEngine.Templating中使用RazorEngineService,例如在我的情况下,它看起来像这样:
using RazorEngine.Templating; RazorEngineService.Create().RunCompile(html, ...)
Assuming you are using the same assembly, @Html.Raw does NOT exist with this usage.
假设您使用相同的程序集,@ Html.Raw不存在此用法。 I > was finally able to get raw HTML output by doing this in my e-mails:
I>终于能够在我的电子邮件中通过这样做获得原始HTML输出:
@using RazorEngine.Text @(new RawString(Model.Variable))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.