Search code examples
c#asp.net-mvcurl.actionpostal

Use Url.Action to generate fully qualified anchor


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

Solution

  • 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