[英]Creating an extension method in C#
我正在嘗試在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 :
HtmlHelper類的擴展名位於System.Web.Mvc.Html命名空間中。 這些擴展添加了輔助方法,用於創建表單,呈現HTML控件,呈現部分視圖,輸入驗證等。
嘗試包含System.Web.Mvc.Html
命名空間。 LinkExtensions.RouteLink給出了它的名稱空間(它表示它位於System.Web.Mvc.dll
,只是在不同的名稱空間中)。
您需要為HtmlHelper
引用System.Web.Mvc
,但擴展名在System.Web.Mvc.Html
。
您的命名空間可能有誤。 嘗試using System.Web.Mvc.Html
;
http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper(VS.90).aspx
我不確定添加命名空間請參閱resukt System.Web.Mvc.Html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.