c# - Regex.Replace 或 String.Replace 不适用于 Linux 环境中的主机 URL
问题描述
我的安装
开发机器:
- 视窗 10
- IIS 快递
分期机:
- Ubuntu 18.04
- Nginx
我的问题
我有一个简单但奇怪的问题。在下面的代码块中,我生成了一个电子邮件确认链接。现在,我想用另一个替换生成的 URL 的域。
// class fields
private readonly string _WebServerRelativeUrl = "https://www.specific-domain.com/account/";
private readonly string _BingoServerRelativeUrl = "https://www.specific-domain.eu/api/v1/identity/";
private readonly string _BingoLocalServerRelativeUrl = "https://localhost:44375/api/v1/identity/";
// code excerpt from confirm email method
var passwordResetLink = _urlHelper.Action("ResetPassword", "identity",
new { email = appUser.Email, token, lang }, _httpRequest.HttpContext.Request.Scheme);
var url = Regex.Replace(passwordResetLink, _BingoServerRelativeUrl, _WebServerRelativeUrl, RegexOptions.IgnoreCase);
if (_environment.IsDevelopment())
{
url = Regex.Replace(passwordResetLink, _BingoLocalServerRelativeUrl, _WebServerRelativeUrl, RegexOptions.IgnoreCase);
}
在本地主机上,一切正常。服务器生成如下 URL:
https://localhost:44375/api/v1/identity/confirmemail?userId=abcd&token=abcd
然后将 https://localhost:44375/api/v1/identity 成功替换为https://www.specific-domain.com/account/
但是,在生产环境中,该应用程序在 Ubuntu 18.04 上运行,位于具有特定域的 Nginx 后面。现在,当应用程序位于 Linux VM 上的生产环境中时,此字符串替换不起作用。
我还尝试了 String.Replace() 而不是正则表达式,结果相同。可能是什么问题?
解决方案
调试这个问题需要相当多的时间。通过使用Flurl 库构建最初所需的 URL,找到了一个更简单的解决方案。该解决方案需要更少的代码。
var url = WebServerRelativeUrl
.AppendPathSegment(ResetPassPath)
.SetQueryParams(new
{
email = appUser.Email, token = token, lang = lang
});
PS 我更喜欢这个而不是UriBuilder,因为它提供了流畅的界面。
推荐阅读
- javascript - React JS 中的 window.addeventlistener('resize') componentDidMount 循环
- r - 每个值的列中返回值 = 给定月份
- python - 删除python集中的部分字符串
- python - 用列表中的唯一值替换数据框中列中的 NAN 值?
- django - 在 django 中,我无法理解包含函数的工作原理
- mysql - 在 MongoDB 中的同一查询中查找和计数
- html - 在 svg 中居中文本
- android - Compose 运行时中的 `isLiveLiteralsEnabled` 函数是什么,如何禁用它?
- pandas - 如何决定是保留重复行还是删除它们。我有两条重复的记录,但它们指的是两个不同的人
- node.js - 命名空间“NodeJS”没有导出成员“Require”