Avoiding the use of the “use” clause in razor mode

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

@using MyWebPage.Helpers 

Is there any way to avoid this? It would be great if this import is automatic.

I added this to web.config as shown 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 that would not solve my problem.

+4
source share
2 answers

There are 2 web.configs in the project.

  • For views
  • For applications

You need to include the namespace in the correct web.config, which is for submissions. When you include it in the correct namespace, you do not need to include it on 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> 
+8
source

Add a namespace to Views/web.config , close all cshtml files, close them, voila.

+5
source

All Articles