Alxandr
Alxandr

Reputation: 12431

Generating absolute urls from Html.RouteLink

I'm using @Html.RouteLink("Validate", "ValidateEmail", new { email = Model.Email, key = Model.Key }) to generate a link that I send in an email to newly registered user. And as I was trying this out I discovered that, wops, it didn't generate absolute urls... This means I got a link like <a href="/Membership/Validate/email.to%40you.com?key=someKey">Validate</a>. However, as I send this link in an email it (of cause) won't work, so how do I go about making this better? Any simple solutions?

Upvotes: 4

Views: 2104

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

You can use the following overload:

@Html.RouteLink(
    "click me", 
    "Default", 
    "http", 
    null, 
    null, 
    new { 
        email = Model.Email, 
        key = Model.Key, 
        controller = "Home", 
        action = "Index" 
    }, 
    null)

Default is the route name as defined in Global.asax, http is the protocol (could be https if you wish, or simply use Request.Url.Scheme to get the containing page protocol and avoid ugly hardcodings), then an anonymous object containing the route values and that's pretty much all -> you will get an absolute url.

By the way all url generating helpers have overloads which take the protocol and hostname: use those for absolute urls.

Upvotes: 7

Related Questions