Как получить ссылки на URL-адреса в тексте в ASP.NET MVC 4 с синтаксисом Razor?

У меня есть модель с текстовым полем. ТекстМожно содержат несколько URL-адресов. Он не должен содержать URL-адреса и не имеет определенного формата.

С помощью

@Html.DisplayFor(model => model.TextWithSomeUrls)

текст и URL отображаются как обычный текст, конечно. Я хотел бы, чтобы URL отображались как рабочие индивидуальные ссылки. Есть ли вспомогательный метод для этого в ASP.NET / Razor?

редактироватьПрямо сейчас вывод:

http://www.google.com, foo: bar;  http://www.yahoo.com

Что именно содержание текстового поля.

Но я хочу получить URL-адреса и только URL-адреса, отображаемые как ссылки:

<a href="http://www.google.com">http://www.google.com</a>, foo: bar; <a href="http://www.yahoo.com">http://www.yahoo.com</a>

Мое решение:

public static partial class HtmlExtensions
{
    private const string urlRegEx = @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)";

    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();
    }
}

Это расширение теперь можно использовать в представлениях:

@Html.DisplayWithLinksFor(model => model.FooBar)

Ответы на вопрос(4)

Ваш ответ на вопрос