简体   繁体   English

从类库添加对Web项目的引用

[英]Add references to web project from a class library

I have a class library that have folder Called "Resources" that's our convention of keeping the DLL that need to be referenced. 我有一个类库,其中有一个名为“ Resources”的文件夹,这是我们保留需要引用的DLL的约定。

So there are some base DLL that are added in it.Lets name them as "BaseWork.dll". 因此,其中添加了一些基本DLL。将其命名为“ BaseWork.dll”。

I have a web project in the same solution that have this class library. 我在具有此类库的相同解决方案中有一个Web项目。 I have added the reference of Class library in the Web project. 我在Web项目中添加了类库的引用。 I want to access some class through this "BaseWork.dll" in the code behind of some aspx page. 我想通过aspx页面后面的代码中的“ BaseWork.dll”访问某些类。 I am not able to access it. 我无法访问它。

There is an easiest way to do this is Add the Resource folder for our convention Sake , add the "BaseWork.dll" to it. 最简单的方法是为我们的约定Sake添加Resource文件夹,向其中添加“ BaseWork.dll”。 Then add the reference in the Web-project . 然后在Web项目中添加引用。 That would be repetition of the same things across the solution. 那将是整个解决方案重复相同的事情。

Is there any better way to do this? 有什么更好的方法吗?

PS: I am working in .net framework 4.0 in Visual studio 2010. PS:我正在Visual Studio 2010的.net Framework 4.0中工作。

I want to access some class through this "BaseWork.dll" in the code behind of some aspx page 我想通过aspx页面后面的代码中的“ BaseWork.dll”访问某些类

2 things to do in your web.config: 在您的web.config中要做的2件事:

  1. Add the assembly to the <assemblies> section: 将程序集添加到<assemblies>部分:

     <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="BaseWork" /> </assemblies> </compilation> 
  2. Add the namespace containing the class you need to use to the <namespaces> section: 将包含需要使用的类的名称空间添加到<namespaces>部分:

     <pages> <namespaces> <add namespace="BaseWork.SomeNamespace" /> </namespaces> </pages> 

Now inside any WebForm you will be able to use classes within the namespace you declared. 现在,在任何WebForm中,您都可以在声明的名称空间中使用类。 As an alternative to modifying the web.config you could also reference it inside a specific WebForm: 作为修改web.config的替代方法,您还可以在特定的WebForm中引用它:

<%@ Assembly Name="BaseWork" %>
<%@ Import Namespace="BaseWork.SomeNamespace" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM