跨网站使用相同的 App_Code 类
假设您有一个包含两个网站项目的解决方案:网站 A 和网站 B。现在,在网站 A 的 App_Code 文件夹中,ClassX.cs 文件中定义了一个类 X。 如果B网站也需要访问ClassX.cs怎么办?
有什么方法可以跨 App_Code 文件夹共享此文件吗? 假设将文件移动到公共库是不可能的。
Let's say you have a solution with two website projects, Website A and Website B. Now inside Website A's App_Code folder, there is a Class X defined in a ClassX.cs file. What do you do if Website B also needs access to ClassX.cs?
Is there any way to share this file across App_Code folders? Assume that moving the file to a common library is out of the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请不要使用这些邪恶的网站项目。 请改用 Web 应用程序项目,将共享类打包到库项目中,并从所有 Web 应用程序中引用它。
Please please don't use these unholy website projects. Use Web Application projects instead, pack your shared classes into a library project and reference it from all your Web Applications.
将共享类打包到库(DLL)中,然后从每个站点右键单击“添加引用”并选择您创建的库。
Pack your shared classes into a Library (a DLL) and from each site right-click on add reference and select the library that you have created.
受到“假设将文件移动到公共库是不可能的”的限制。 执行此操作的唯一方法是使用 NTFS 连接点本质上创建一个符号链接,以便在两个文件夹中具有相同的 .cs 文件。
不过,这是一个糟糕的选择(出于版本控制的原因)...将其移动到公共库是最好的选择。
这是关于 NTFS 连接点的维基百科条目
http://en.wikipedia.org/wiki/NTFS_junction_point
这是创建它们的工具
http://technet.microsoft.com/en-us/sysinternals/bb896768。 ASPX
With the restriction of "Assume that moving the file to a common library is out of the question." the only way you could do this is to use NTFS junction points to essentially create a symlink to have the same .cs file in both folders.
This is a terrible option though (for versioning reasons)...moving it to a common library is the best option.
Here's the Wikipedia entry on NTFS junction points
http://en.wikipedia.org/wiki/NTFS_junction_point
and here's a tool for creating them
http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx
我不相信有一种方法可以不将 ClassX 移动到新的代码库项目中。 .NET 要求程序集的所有依赖项与程序集本身存在于同一文件夹中,或者存在于 GAC 中,以便自动检测。
您可以尝试通过反射类手动加载程序集,尽管这有点hacky。
如果您有时间并且愿意承担的话,最好的解决方案是采用 JRoppert 的解决方案,将其迁移到 Web 应用程序项目。 然后,您可以使用 Web 引用(其工作方式与 VS 内的常规引用一样好)来引用 ClassX。
华泰
I don't believe that there is a way without moving ClassX into a new code library project. .NET requires all an assembly's dependencies to exist in the same folder as the assembly itself, or in the GAC, to be automatically detected.
You could try loading the assembly manually via the Reflection classes, although it's a bit hacky.
The best solution, if you have the time available and the inclination to undertake it, would be to go with JRoppert's solution of moving it to a web application project. You could then use web references (which work about as nicely as regular references inside VS) to refer to ClassX.
HTH