How do I include a class library in an ASP.NET Razor website (not MVC) so that I don't have to use the using statement?

I have a C # ASP.Net Razor page (not MVC) ...

@using QuickCodeLearning.Customers.Utilities; @{ var cus = CustomerUtilities.GetCustomerInformation(1); } <html> <head> <title> Display a Customer </title> </head> <body> <p>@cus.fname @cus.lname @cus.FavFruit</p> </body> </html> 

I enable the class library (QuickCodeLearning.Customers.Utilities) using the using statement.

Everything is working fine.

enter image description here

My question is: can I add this class library to my Web.Config file so that I don't have a using statement at the top of each page?

Here is my Web.Config file ...

 <?xml version="1.0"?> <configuration> <connectionStrings> <add name="CustomersEntities" connectionString="metadata=res://*/CustomersEF.csdl|res://*/CustomersEF.ssdl|res://*/CustomersEF.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=LT5V6V8W1\SQLSERVERROCKS;initial catalog=sandbox;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> <system.web> <authentication mode ="Windows"/> <identity impersonate="true"/> <compilation debug="true" targetFramework="4.0"/> </system.web> </configuration> 

I tried adding <page> <& namespace GT; to a Web.Config file like this ...

 <?xml version="1.0"?> <configuration> <pages> <namespaces> <add namespace="QuickCodeLearning.Customers.Utilities" /> </namespaces> </pages> <connectionStrings> <add name="CustomersEntities" connectionString="metadata=res://*/CustomersEF.csdl|res://*/CustomersEF.ssdl|res://*/CustomersEF.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=LT5V6V8W1\SQLSERVERROCKS;initial catalog=sandbox;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> <system.web> <authentication mode ="Windows"/> <identity impersonate="true"/> <compilation debug="true" targetFramework="4.0"/> </system.web> </configuration> 

But that does not work. When I delete the using statement, the page stops working.

What am I missing?

Update:

The answer (which I'm sure is correct) does not work for me. My project does not have a Views folder. When I add a new folder to my web project and turn on the App.Config settings, it does not solve my problem. I closed and updated Visual Studio. I restarted IIS.

I noticed that the views folder is created when Visual Studio 2012 creates an MVC project, however I am not working with the MVC site here. I have a basic website that uses the Razor engine.

+6
source share
4 answers

In the Web.Config file in the Views folder (and not in the main Web.Config in the root of the project), find this section:

 <system.web.webPages.razor> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> ... </namespaces> </pages> </system.web.webPages.razor> 

Here you can add your extra namespace and it will appear in all views.

+6
source

In your web.config follow these steps:

 <system.web.webPages.razor> <host factoryType="System.Web.WebPages.Razor.WebRazorHostFactory, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.WebPages.WebPage"> <namespaces> <add namespace="System.Web.Configuration" /> <add namespace="System.Web.Optimization" /> <add namespace="System.Web.Routing" /> <add namespace="QuickCodeLearning.Customers.Utilities" /> </namespaces> </pages> </system.web.webPages.razor> 
+1
source

I found some interesting things (add namespaces with Razor) in this link http://weblogs.asp.net/mikaelsoderstrom/add-namespaces-with-razor

The idea is to add a namespace to all pages:

we create a new PreApplicationStart class, for example:

 using Microsoft.WebPages.Compilation; public class PreApplicationStart { public static void InitializeApplication() { CodeGeneratorSettings.AddGlobalImport("QuickCodeLearning.Customers.Utilities"); } } 

Add the following code to AssemblyInfo.cs:

 [assembly: PreApplicationStartMethod(typeof(PreApplicationStart), "InitializeApplication")] 

When we launch our site, InitializeApplication will be launched before Application_Start in global.asax

Hope this helps

0
source

You need to put them in the correct <system.web> section in the web.config file. eg.

  <configuration> <system.web> <pages> <namespaces> <add namespace="System.Data" /> <add namespace="System.Text"/> </namespaces> </pages> </system.web> </configuration> 

The purpose of a namespace section is to bypass import execution on an ASPX page and another active server page. C # code still requires that you use the using statements at the top of your .cs file.

0
source

All Articles