问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/33086067/add-a-namespace-reference-to-all-web-application-web-pages-in-a-different-projec