I have a C# ASP.Net Razor (non MVC) webpage...
@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'm including a class library (QuickCodeLearning.Customers.Utilities) via a using statement.
Everything works fine.
My question is can I add this class library to my Web.Config file so that I don't have to have the 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="data source=LT5V6V8W1\SQLSERVERROCKS;initial catalog=sandbox;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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> to the 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="data source=LT5V6V8W1\SQLSERVERROCKS;initial catalog=sandbox;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<system.web>
<authentication mode ="Windows"/>
<identity impersonate="true"/>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
But this does not work. When I remove the using statement the page stops working.
What am I missing?
Update:
The answer (which I'm sure is correct) is not working for me. My project does not have a Views folder. When I add a new folder to my web project and included the App.Config settings it does not solve my issue. I closed and repopened Visual Studio. I restarted IIS.
I noticed that a views folder is created when you have Visual Studio 2012 create a MVC project, however I'm not working with a MVC site here. I have a basic website that is using the Razor engine.
In the Web.Config
file in Views
folder (not the main Web.Config
in project's root) , 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 additional Namespace and it will be on all Views.
In your web.config, do like this:
<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>
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 namespace to all pages :
we create a new class called PreApplicationStart , like this :
using Microsoft.WebPages.Compilation;
public class PreApplicationStart
{
public static void InitializeApplication()
{
CodeGeneratorSettings.AddGlobalImport("QuickCodeLearning.Customers.Utilities");
}
}
Add the following code in AssemblyInfo.cs:
[assembly: PreApplicationStartMethod(typeof(PreApplicationStart), "InitializeApplication")]
When we start our website, InitializeApplication will be executed before Application_Start in global.asax
Hope it helps
You need to put them in the correct <system.web>
section of web.config. eg
<configuration>
<system.web>
<pages>
<namespaces>
<add namespace="System.Data" />
<add namespace="System.Text"/>
</namespaces>
</pages>
</system.web>
</configuration>
The purpose of the namespace section is to get around having to do the import in the .aspx and other active server page. Code behind in C# still requires you to have the using statements at the top of your .cs file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.