简体   繁体   English

这是MVC3中的扩展或帮助方法吗?

[英]Is this an Extension or Helper Method in MVC3?

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? 例如,以下是返回MvcHtmlString的扩展名还是辅助方法?

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帮助程序。在C#中没有“辅助方法”实现。 HTML helpers are implemented as extension methods. HTML帮助程序实现为扩展方法。 You can see extension methods are static methods with a this clause before first parameter. 您可以在第一个参数之前看到扩展方法是带有此子句的静态方法。 HTML helpers makes it easier to generate html tags. HTML帮助程序可以更轻松地生成html标记。

public static MvcHtmlString HdrLinks(this HtmlHelper helper, string topLink)

is both a C# extension method and an html helper method for use in ASP.NET MCV Views: 是一个C#扩展方法和一个用于ASP.NET MCV视图的html辅助方法:

//Example of a call as an extension method:

var helper = new HtmlHelper(...);
var result = helper.HdrLinks(topLink);

//Example of a call as a helper method in an MVC razor view:
@Html.HdrLinks(topLink)

The following is a "standard" static C# method: 以下是“标准”静态C#方法:

public static class Slug {
  public static string Generate(string phrase, int maxLength = 50)
}

//Example of call:

var phrase = "Freech Alpes are the surfer's best spot"
var result = Slug.Generate(phrase);

Static methods are called without a class instance. 在没有类实例的情况下调用静态方法。 It is a special dummy construct to group functions. 它是一个特殊的虚拟构造来对函数进行分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM