Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include a class library into a (non MVC) ASP.net Razor website so that I don't have to use the using statement?

Tags:

c#

razor

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.

enter image description here

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=&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> 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=&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 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.

like image 774
codingguy3000 Avatar asked Mar 02 '16 15:03

codingguy3000


2 Answers

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.

like image 106
teo van kot Avatar answered Oct 11 '22 08:10

teo van kot


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>
like image 22
Alexandre N. Avatar answered Oct 11 '22 06:10

Alexandre N.