Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" />
  </namespaces>
</pages>

But it wouldn't solve my problem.

like image 837
nosbor Avatar asked Dec 12 '22 10:12

nosbor


2 Answers

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>
like image 180
Mangesh Pimpalkar Avatar answered Jan 13 '23 00:01

Mangesh Pimpalkar


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

like image 24
Lukáš Novotný Avatar answered Jan 13 '23 01:01

Lukáš Novotný