使用Url.Action生成完全限定的锚点

4

我正在使用Url.Action在我的应用程序中生成链接,用于发送电子邮件(使用Postal MVC框架),但是生成的链接显示为“localhost”名称,而不是域名。

我正在使用以下代码:

@Url.Action("AlterarSenha", "Account", null, this.Request.Url.Scheme)

以下是结果:
http://localhost/Account/AlterarSenha

之后,我尝试了以下代码:

@Url.Action("AlterarSenha", "Account", null, this.Request.Url.Scheme, Request.ServerVariables["HTTP_HOST"])

我得到了相同的结果。

我该如何获得与我的域名相关的链接,例如:

http://www.servicili.com/Account/AlterarSenha

@Url.Action("AlterarSenha", "Account") - user3559349
结果:http://account/AlterarSenha - Dan
2个回答

3

正如您所看到的,这是我使用的代码,但是我想知道是否可能让APP确定主机名,而不是将其放在Url.Action中。 - Dan
除非您的电子邮件发送类中有访问 HttpContext.Current.Request 对象,该对象具有 UserHostName 属性和其他信息,否则无法完成此操作。 - Fayyaz Naqvi

1
如果您的本地服务器使用80端口,您可以使用:

Url.Action("AlterarSenha", "Account",null, null, "www.servicili.com");

结果是:
http://www.servicili.com/Account/AlterarSenha

如果你的项目使用了其他端口(例如 123),那么结果将为:
 http://www.servicili.com:123/Account/AlterarSenha

你可以将协议设置为https,这不会添加任何端口。
Url.Action("AlterarSenha", "Account",null, "https", "www.servicili.com");

结果是

https://www.servicili.com/Account/AlterarSenha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接