这是MVC3分机或helper方法? [英] Is this an Extension or Helper Method in MVC3?

查看:119
本文介绍了这是MVC3分机或helper方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑哪个是哪个。有人可以解释两者之间的区别。

例如是返回一个MvcHtmlString分机或一个辅助方法下面?

 公共静态类LinkExtensions
{
    公共静态MvcHtmlString HdrLinks(
        这个的HtmlHelper帮手,
        串TOPLINK,
        串SUBLINK,
        System.Security.Principal.IPrincipal用户)
    {
    等等...

这个怎么样:

 公共静态类弹头
{
    公共静态字符串生成(串词,INT最大长度= 50)
    {
        字符串str = RemoveAccent(短语).ToLower();
        海峡= Regex.Replace(STR,@[^ A-Z0-9 \\ S-],);
        海峡= Regex.Replace(STR,@[\\ S - ] +,).Trim();
        海峡= str.Substring(0,str.Length< =最大长度str.Length:最大长度).Trim();
        海峡= Regex.Replace(STR,@\\ S, - );
        返回海峡;
    }


解决方案

他们不是辅助方法,它们被称为HTML helpers.There在C#中没有'helper方法实施。 HTML辅助实现为扩展方法。你可以看到扩展方法与第一个参数之前,这一条款的静态方法。 HTML辅助使得它更容易生成HTML标签。

I am confused about which is which. Can someone explain the difference between the two.

For example is the following which returns a MvcHtmlString an extension or a helper method?

public static class LinkExtensions
{
    public static MvcHtmlString HdrLinks(
        this HtmlHelper helper, 
        string topLink, 
        string subLink, 
        System.Security.Principal.IPrincipal user)
    {
    etc ...

How about this:

    public static class Slug
{
    public static string Generate(string phrase, int maxLength = 50)
    {
        string str = RemoveAccent(phrase).ToLower();
        str = Regex.Replace(str, @"[^a-z0-9\s-]", " ");
        str = Regex.Replace(str, @"[\s-]+", " ").Trim();
        str = str.Substring(0, str.Length <= maxLength ? str.Length : maxLength).Trim();
        str = Regex.Replace(str, @"\s", "-");
        return str;
    }

解决方案

They are not helper methods, they are called HTML helpers.There is no 'helper method' implementation in C#. HTML helpers are implemented as extension methods. You can see extension methods are static methods with a this clause before first parameter. HTML helpers makes it easier to generate html tags.

这篇关于这是MVC3分机或helper方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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