如何将 SharePoint 2007 功能挂接到网站的 Application_Start 中?
我想知道在开发功能时是否有一种好方法可以挂钩 SharePoint 2007 网站的 Application_Start? 我知道我可以直接编辑站点根目录中的 Global.asax 文件,但是有没有办法做到这一点,以便它与该功能一起部署?
谢谢!
I was wondering if there is a good way to hook into the Application_Start of a SharePoint 2007 site when developing a feature? I know I can directly edit the Global.asax file in the site root, but is there a way to do this so that it gets deployed with the feature?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这其实是可以的,但是不涉及Global.asax文件。
Microsoft 的许多示例都演示了通过 Global.asax 连接代码,但这并不是 SharePoint 的最佳实践方法。 理想情况下,您的代码应打包为功能并通过 WSP 进行部署(正如您所知)。
关键在于将相关代码实现为 HttpModule(即实现 IHttpModule 接口的类型)并将其连接到为 SharePoint 应用程序提供服务的 ASP.NET 管道中。 粗略地说,步骤如下:
有关 HttpModule 开发的详细信息,请参阅 http://msdn.microsoft.com/ en-us/library/ms227673.aspx。 有关 SPWebConfigModification 类型的其他详细信息,请参阅 http: //msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx。
结果:一个可以处理应用程序启动并可通过功能部署的类。 无需手动破解文件。
我已经在许多场景中成功地使用了它——最近的一个自定义缓存提供程序 (IVaryByCustomHandler) 需要在启动时向 SPHttpApplication 注册自己以进行回调。
虽然你的问题有点老了,但我希望这会有所帮助!
This is actually possible, but it doesn't involve the Global.asax file.
Many of Microsoft's examples demonstrate wiring code in via the Global.asax, but this is not a best-practices approach when it comes to SharePoint. Ideally, your code should get packaged as a Feature and deployed via WSP (as you already know).
The key lies in implementing the code in question as an HttpModule (i.e., a type that implements the IHttpModule interface) and wiring it into the ASP.NET pipeline servicing your SharePoint application. Roughly speaking, these are the steps:
For more information on HttpModule development, see http://msdn.microsoft.com/en-us/library/ms227673.aspx. For some additional detail on the SPWebConfigModification type, see http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx.
Result: a class that can handle application startup and is deployable via Feature. No manual file hacking required.
I've successfully used this in a number of scenarios -- most recently with a custom caching provider (IVaryByCustomHandler) that needed to register itself for callbacks with the SPHttpApplication when it started.
Though your question is a bit older, I hope this helps!
我的直觉是这是不可能的。 Application_Start 在 asp.net 引擎启动时由运行时调用,因此除了修改 Global.asax 之外,很可能没有任何方法可以挂钩处理程序 - 例如,挂钩必须是声明性的和持久性的,因为它必须在应用程序停止/卸载后继续存在。 因此,如果您必须写入 global.asax,我想您可以编写一个 Feature EventReceiver 来执行修改。
除此之外,您能详细说明原因吗? 也许还有其他的攻击角度。 即时修改 global.asax 的想法让我感到不舒服。 那可不太好。
奥辛
My gut feeling on this is that it won't be possible. Application_Start is called by the runtime as the asp.net engine is starting up, so there most likely can't be any way to hook the handler outside of modifying the Global.asax - e.g. the hook must be declarative and persistent as it has to survive the application stopping/unloading. So, if you have to write to the global.asax, I guess you could write a Feature EventReceiver to perform the modification.
That aside, can you give more details on the why? Perhaps there are other angles of attack. The idea of modifying the global.asax on the fly makes me feel ill. That can't be good.
Oisin