Skip to main content
 首页 » 编程设计

c#之扩展方法调用在 MVC 中不起作用

2024年08月12日57mfryf

我创建了一个扩展方法来通过 MVC 呈现纯 HTML 链接,如 this question :

namespace MyProj 
{ 
    public static class HtmlHelpers 
    { 
        public static string SectionLink(this HtmlHelper html, string URL, string display) 
        { 
            return String.Format("<a href=\"{0}\">{1}</a>", URL, display); 
        } 
    } 
} 

我在 web.config 中添加了引用,views/web.config 可以在页面中引用它,Visual Studio 不会给出任何错误:

<h1 class="site-title">@HtmlHelpers.SectionLink("https://stackoverflow.com/", "Home")</h1> 

一旦我运行该项目,我得到一个错误:

Compiler Error Message: CS1501: No overload for method 'SectionLink' takes 2 arguments 

我卡在了这一点上。 Visual Studio 对引用很满意,但根据错误消息判断它似乎没有识别出它是一种扩展方法。我做错了什么?

请您参考如下方法:

将方法作为扩展方法调用:

@Html.SectionLink("abc", "xyz")