larryq
larryq

Reputation: 16299

Create external link in Asp.Net MVC3

I know there are a lot of utility and helper classes/methods for generating URLs and links from internal routes and controllers. But how would you tackle the below in MVC 3?

In a razor file someone has defined this:

<a href="http://@Model.Details.ExternalURL" target="_blank">Website</a>

ExternalURL in this instance will hold values like www.yoursite.com, without any prefixes. Hard-coding an http:// at the start is an obvious no-no but how best to handle this?

Upvotes: 0

Views: 2253

Answers (1)

Perevalov
Perevalov

Reputation: 348

It's not so bad to hardcode http:// in your case, but if you want to avoid it, I see few options, but maybe most correct will be to extend your model with property @Model.Details.ExternalUrlLink or something like that. In getter you can do any logic what you want over original value, e.g. concatenate http:// prefix if it's not presented

Upvotes: 1

Related Questions