如何获得与剃刀语法​​链接到URL文本在ASP.NET MVC 4? [英] How to get links to URLs in text in ASP.NET MVC 4 with Razor syntax?

查看:108
本文介绍了如何获得与剃刀语法​​链接到URL文本在ASP.NET MVC 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字段的典范。文本的可以包含多个URL。它不必包含URL和它不具有特定的格式。

使用

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

文字和网址都显示像当然普通文本。我想获得显示为工作虽然单个链接的URL。有没有在ASP.NET /剃刀?

这一个辅助方法

修改:现在的输出是:

  http://www.google.com,富:酒吧; http://www.yahoo.com

这是完全文本字段的内容。

不过,我想呈现为这样的链接的URL,只有网址:

 &LT; A HREF =htt​​p://www.google.com&GT; HTTP://www.google.com< / A&gt;中富:酒吧; &所述; A HREF =htt​​p://www.yahoo.com&GT; HTTP://www.yahoo.com< / A&GT;

我的解决方案

 公共静态部分类HtmlExtensions
{
    私人常量字符串urlRegEx = @\"((http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?)\";    公共静态MvcHtmlString DisplayWithLinksFor&LT;的TModel,TProperty&GT;(此的HtmlHelper&LT;的TModel&GT;的HtmlHelper,防爆pression&LT; Func键&LT;的TModel,TProperty&GT;&GT;前pression)
    {
        字符串内容=的getContent&LT;的TModel,TProperty&GT;(的HtmlHelper,前pression);
        字符串结果= ReplaceUrlsWithLinks(内容);
        返回MvcHtmlString.Create(结果);
    }    私人静态字符串ReplaceUrlsWithLinks(字符串输入)
    {
        正则表达式RX =新的正则表达式(urlRegEx);
        字符串结果= rx.Replace(输入,委托(匹配匹配)
        {
            字符串的URL = match.ToString();
            返回的String.Format(&下; A HREF = \\{0} \\&GT; {0}&下; / A&gt;中,URL);
        });
        返回结果;
    }    私人静态字符串的getContent&LT;的TModel,TProperty&GT;(的HtmlHelper&LT;的TModel&GT;的HtmlHelper,防爆pression&LT; Func键&LT;的TModel,TProperty&GT;&GT;前pression)
    {
        FUNC&LT;的TModel,TProperty&GT; FUNC =前pression.Compile();
        返回FUNC(htmlHelper.ViewData.Model)的ToString();
    }
}

此扩展,现在可以在视图中使用:

  @ Html.DisplayWithLinksFor(型号=&GT; model.FooBar)


解决方案

有像没有帮手,但你可以创建自己的自定义帮手或创建一个<一个href=\"http://www.hanselman.com/blog/ASPNETMVCDisplayTemplateAndEditorTemplatesForEntityFrameworkDbGeographySpatialTypes.aspx\"相对=nofollow>模板DisplayFor帮手,其中将包含你需要的逻辑。

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\-\.,@?^=%&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();
    }
}

This extension can now be used in views:

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

解决方案

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.

这篇关于如何获得与剃刀语法​​链接到URL文本在ASP.NET MVC 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆