5

I have a solution with three projects:

  • Project 1 - Reusable Logic/Methods (applicable to all other projects)
  • Project 2 - Web site
  • Project 3 - Web application (project 2 being converted to a web application)

In project 1 I have this code:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Runtime.CompilerServices

Namespace MyCompany

    Public Module DataExtensionMethods

        <Extension()> _
        Public Function SortByColumn(ByVal dt As DataTable, ByVal SortString As String) As DataTable
            ' Amazing stuff here...
            Return dt
        End Function

    End Module

End Namespace

In the old-fashioned Project 2 web site, this extension method was easily accessible by adding the following to web.config:

<pages controlRenderingCompatibilityVersion="4.0" validateRequest="true" clientIDMode="AutoID">
    <namespaces>
        <add namespace="MyCompany.DataExtensionMethods"/>
    </namespaces>
</pages>

The extension method immediately became available on all references to a DataTable variable.

However, I cannot get this to work in Project 3 (web application) on an application-wide scale.

In each page, I can add this:

Imports MyCompany.DataExtensionMethods

This then makes the extension method available, but how do I achieve the same application-wide, without having to use Imports on each and every page?

In both Project 2 and 3 I have defined a reference to Project 1 using the Add Reference dialog. The check box shows that still exists, but Project 3 cannot see Project 1's logic. There seems to be loads of people seeing the same behaviour with WebAPI, but not with web page apps.

Community
  • 1
  • 1
EvilDr
  • 8,943
  • 14
  • 73
  • 133

1 Answers1

3

In VB, you can set a globally imported namespace for the project by right clicking your project in the solution explorer, choosing properties.

On the references tab, at the bottom is the Imported namespaces section. You can add your namespace MyCompany.DataExtensionMethods there and it will act as if you're typed Imports MyCompany.DataExtensionMethods in all your source files.

enter image description here

Erik
  • 5,355
  • 25
  • 39
  • 1
    OP is NOT asking how to add a reference to the project. His intention is to use members of MyCompany.DataExtensionMethods namespace without using `Imports MyCompany.DataExtensionMethods` (and implicitly, without using fully qualified names) – haraman Oct 12 '15 at 17:32
  • 1
    @haraman Yes, that's what the **Imported namespaces** section is for: creating a global namespace import. – Erik Oct 12 '15 at 17:33