4
votes

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
@Url.Action("AlterarSenha", "Account")user3559349
Result: http://account/AlterarSenhaDan

2 Answers

3
votes

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
votes

if your local server uses port 80 you can use

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

Result is

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

If your project uses any other port (like 123) the result will be

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

Also you may set protocol as https it won't add any port

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

The result is

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