Reputation: 1624
In my database I have URLs starting with http://SOMEURL
and some are just www.SOMEURL.
in my view, ‚if I just put <a href="@Model.URL">
it wont work properly.
It adds http://localhost/SOMEURL
when I try to browse locally.
How do I handle this BASIC functionality in ASP ?
google.com https://www.google.com
Upvotes: 0
Views: 43
Reputation: 5847
You should check for http prefix manually. URLs will be encoded automatically (e.g. replacing special characters) in ASP.NET MVC.
In Razor View (.cshtml; Anywhere on top of file)
@functions
{
public string PrefixUrl(string url)
{
return url.StartsWith("http") ? url : string.Format("http://{0}", url);
}
}
I wish we could assume https nowadays but probably, using http is safer.
Then, in View, use:
<div>
<a href="@PrefixUrl(Model.Url)">Link</a>
</div>
Upvotes: 1