开发者

Avoiding using a "using" clause in razor view

I wrote a UrlHelper and in every view I need to include an using clause:

@using MyWebPage.Helpers

Is there way to avoid it? That would be great if this import automatically.

I added this in web.config as below:

<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <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.Web.WebPages" />
    <add namespace="MyWebPage.Helpers" />
 开发者_如何学JAVA </namespaces>
</pages>

But it wouldn't solve my problem.


There are 2 web.configs in the project.

  1. For Views
  2. For application

You need to include the namespace in proper web.config which is for views. Once you include it in proper namespace you don't have to include it in every page.

<system.web.webPages.razor>
<host ....>
<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <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.Web.WebPages" />
    <add namespace="MyWebPage.Helpers" />
  </namespaces>
</system.web.webPages.razor>
</pages>


Add the namespace into Views/web.config, close all cshtml files, reopen them, voila.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜