4

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
Dan
  • 1,518
  • 5
  • 20
  • 48

2 Answers2

3

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

Fayyaz Naqvi
  • 695
  • 7
  • 19
  • As you can see, it is the code that I was using, however, I'd like to know if is possible the APP determine the hostname, instead I put it in the Url.Action – Dan Oct 30 '15 at 21:40
  • Not unless you have access to the HttpContext.Current.Request object in your email-sending class, which has the UserHostName property and other info. – Fayyaz Naqvi Oct 30 '15 at 21:47
1

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
MIkhail
  • 31
  • 2