加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中创建扩展方法

发布时间:2020-12-16 01:39:48 所属栏目:百科 来源:网络整理
导读:我正在尝试在C#中为 HtmlHelper类创建一个扩展方法.我已经阅读了它的MSDN页面,我确信我引用了正确的命名空间.我想知道我做错了什么. using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc; //Correctly
我正在尝试在C#中为 HtmlHelper类创建一个扩展方法.我已经阅读了它的MSDN页面,我确信我引用了正确的命名空间.我想知道我做错了什么.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; //Correctly referencing the necessary namespaces,right?

namespace MvcApplication1.HelperMethods
{
    public static class NavigationalMenu
    {
        public static string MyMenu(this HtmlHelper helper)
        {
            CategoryRepository categoryRepo = new CategoryRepository();
            var categories = categoryRepo.FindAllCategories();

            foreach (Category c in categories)
            {
                helper.RouteLink(blablabla); //Construct links and return them.
            }

            //helper.RouteLink doesn't show up! C# wipeouuuuuttttt.
            //It's as if 'helper' doesn't have the RouteLink method there.
        }
    }
}

第一次在C#编程时发生这种情况.其他人遇到这个问题?

解决方法

根据 MSDN:

Extensions to the HtmlHelper class are
located in the System.Web.Mvc.Html
namespace. These extensions add helper
methods for creating forms,rendering
HTML controls,rendering partial
views,input validation,and more.

尝试包含System.Web.Mvc.Html命名空间. LinkExtensions.RouteLink给它的命名空间(它说它在System.Web.Mvc.dll中,只是在不同的命名空间中).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读