Is it possible to use a Global.asax from another assembly?
I am trying to开发者_如何转开发 configure an ASP.NET MVC application that will use one of two MvcApplication derived classes, depending on the context under which the application is running.
The primary web application, which contains all production code, views, etc. has the standard class derived from MvcApplication in global.asax. What I want is to have another class that is used during web testing (Selenium based browser tests). I want all the resources, code, view, etc from the web application to be used except for the global.axax/MvcApplication class.
The testing site needs a different global.asax so that I can set up the container differently, and configure some other settings (e.g., logging, tracing, etc) that are helpful during test, but don't make sense for production.
Does anyone know if this is possible, and if so, how to do it?
Thanks, Erick
I think you can move your bootstrap code in a two difference class one for debug and other for production that implement the same interface : IBootstrapProvider, and in your global asax you initialize One of those depending from a parameter in the web config! This solution works for me!
I understand good your question?
Hope can help you Marco
If the code you have in global.asax is in 1 place (say Application_Start) then the solution that Marco provides is doable. But if you have code blocks in other methods (like Application_BeginRequest, Application_EndRequest) this will be alot harder to move this code out.
In this case I would suggest using a HttpModule. In the HttpModule you can subscribe to the same events as in global.asax, but they are in a different file. Switching the HttpModule on is just commenting out the line(s) in the web.config.
See http://support.microsoft.com/kb/307996 on how to implement and configure a HttpModule. If you need the HttpModule to run on IIS 7 you need to set it differently in web.config: http://pro-thoughts.blogspot.com/2008/04/using-httpmodules-and-httphandlers.html
精彩评论