Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a namespace reference to ALL web application web pages in a different project

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.

like image 371
EvilDr Avatar asked Oct 20 '22 00:10

EvilDr


1 Answers

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

like image 140
Erik Avatar answered Oct 22 '22 21:10

Erik