静态类型不能用作参数

109*_*793 12 c# asp.net-mvc html-helper

我正在关注MVC音乐商店教程,但我在第5 部分第5部分中遇到了Html Helper .

到目前为止,我似乎已经正确地遵循它(如果我错了请纠正我:))...但是我收到以下错误:

'musicStoreMVC.Helpers.HtmlHelper':静态类型不能用作参数

这是我的应用程序的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace musicStoreMVC.Helpers
{
    public static class HtmlHelper
    {
        public static string Truncate(this HtmlHelper helper, string input, int length)
        {
            if (input.Length <= length)
            {
                return input;
            }
            else
            {
                return input.Substring(0, length) + "...";
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果有人能看到我做错了什么,或者如果需要更多信息,我会很感激指针!谢谢.

Vic*_*din 14

只需将您的静态HtmlHelper类重命名为HtmlHelperExtensions.

  • 得到它,因为扩展方法不能添加到`static class` http://stackoverflow.com/questions/249222/can-i-add-extension-methods-to-an-existing-static-class (3认同)