Use Url.Action to generate fully qualified anchor

时间:2015-10-30 21:24:06

标签: c# asp.net-mvc url.action postal

I'm using Url.Action to generate link in e-mails (with the Postal MVC Framework) that was sent by my application, however, the links generates are showing with "localhost" name, and not domain name.

I'm using the following code:

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

The result is the following:

http://localhost/Account/AlterarSenha

After that, I tried the following code:

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

And I got the same result.

How can I get the link with my domain like:

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

2 个答案:

答案 0 :(得分:2)

Assuming that you want to use your domain name in the URL even when the application runs on localhost, you could use this overload of Url.Action:

public virtual string Action(
    string actionName,
    string controllerName,
    RouteValueDictionary routeValues,
    string protocol,
    string hostName
)

And pass your domain name as hostName.

https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx

答案 1 :(得分:0)

如果您的本地服务器使用端口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