I have a model with a text field. The text can contain several URLs. It does not have to contain URLs and it does not have a specific format.
Using
@Html.DisplayFor(model => model.TextWithSomeUrls)
the text and the URLs are displayed like normal text of course. I would like to get the URLs displayed as working individual links though. Is there a helper method for this in ASP.NET / Razor?
Edit: Right now the output is:
http://www.google.com, foo: bar; http://www.yahoo.com
Which is exactly the content of the text field.
But I want to get the URLs and only the URLs rendered as links like this:
<a href="http://www.google.com">http://www.google.com</a>, foo: bar; <a href="http://www.yahoo.com">http://www.yahoo.com</a>
My solution:
public static partial class HtmlExtensions
{
private const string urlRegEx = @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)";
public static MvcHtmlString DisplayWithLinksFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
string content = GetContent<TModel, TProperty>(htmlHelper, expression);
string result = ReplaceUrlsWithLinks(content);
return MvcHtmlString.Create(result);
}
private static string ReplaceUrlsWithLinks(string input)
{
Regex rx = new Regex(urlRegEx);
string result = rx.Replace(input, delegate(Match match)
{
string url = match.ToString();
return String.Format("<a href=\"{0}\">{0}</a>", url);
});
return result;
}
private static string GetContent<TModel, TProperty>(HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
Func<TModel, TProperty> func = expression.Compile();
return func(htmlHelper.ViewData.Model).ToString();
}
}
This extension can now be used in views:
@Html.DisplayWithLinksFor(model => model.FooBar)
I had some issues with the solution:
I used the code from above, extended it a bit and ended up with this:
Obviously not 100% tested for all URL schemes, but seems to work alright.
Example Input:
Output:
Try to write your own Html Helper, like the following.
And use like that
There is no helper like that, but you can create your own custom helper or create a template for DisplayFor helper, which will contain logic you need.
You may use
@Html.Action(actionName)
if text contains mvc URL.