在其他项目中添加对所有Web应用程序网页的名称空间引用 [英] Add a namespace reference to ALL web application web pages in a different project

查看:61
本文介绍了在其他项目中添加对所有Web应用程序网页的名称空间引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个项目的解决方案:

  • 项目1 -可重用的逻辑/方法(适用于所有其他项目)
  • 项目2 -网站
  • 项目3 -Web应用程序(项目2正在转换为Web应用程序)

在项目1中,我有以下代码:

 导入Microsoft.VisualBasic导入System.Data导入System.Data.SqlClient导入System.Configuration导入System.Web导入System.Runtime.CompilerServices命名空间MyCompany公共模块DataExtensionMethods< Extension()>_公共函数SortByColumn(ByVal dt作为DataTable,ByVal SortString作为字符串)作为DataTable'这里的东西很棒...返回dt结束功能终端模块结束命名空间 

在老式的Project 2网站中,可以通过将以下内容添加到web.config中来轻松访问此扩展方法:

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

扩展方法立即可用于所有对DataTable变量的引用.

但是,我无法在整个应用程序范围的项目3(Web应用程序)中使用它.

在每个页面中,我可以添加:

 导入MyCompany.DataExtensionMethods 

这将使扩展方法可用,但是我如何在整个应用程序范围内实现相同的目的,而不必在每个页面上都使用Imports?

在项目2和3中,我都使用添加引用"对话框定义了对项目1的引用.复选框显示该项目仍然存在,但是项目3无法参见项目1的逻辑.似乎有很多人看到

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.

解决方案

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.

这篇关于在其他项目中添加对所有Web应用程序网页的名称空间引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆