How do I use an extension method in an ASP.NET MVC

2019-01-11 16:26发布

How do I access an extension method in an ASP.Net MVC View? In C# I do

using MyProject.Extensions;

and I remember seeing an XML equivalent to put in a view, but I can't find it anymore.

2条回答
萌系小妹纸
2楼-- · 2019-01-11 16:43

In View:

<%@ Import Namespace="MyProject.Extensions" %>

Or in web.config (for all Views):

<pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Linq" />
    <add namespace="System.Collections.Generic" />

    <add namespace="MyProject.Extensions" />
  </namespaces>
</pages>
查看更多
我只想做你的唯一
3楼-- · 2019-01-11 16:45

For pages using Razor / WebPages, you can include a using directive in your .cshtml page.

@using MyBlogEngine;  
查看更多
登录 后发表回答