开发者

Alternative places to register routes to global.asax

It's most common practice to register routes in Application_Start event within global.asax.cs/vb file. But you need to have access to this file to do so. Fine.

I either don't have or don't want to. I'm trying to integrate Asp.net MVC application into a Sharepoint 2010 site and don't want to create my custom global application class that would also register routes for me and change the Sharepoint's Global.asax file and put a different class definition in it. My application wouldn't be accepted because I would be doing unsupported things to Sharepoint.

I was wondering if it was possible to register routes some place else then? There are two a开发者_JAVA技巧lternatives that first popped into my mind:

  1. Write an HttpModule and abuse application start event as well. This way I would inject application start event code without mangling with global.asax that I can't access. Adding another HttpModule in web.config is something I can do.

    Note: Application-level events are not accessible in modules. Bummer. They can only access request-level events.

    Important: ... or so I thought. If you check accepted answer it's actually possible to handle application-level start event. Indirectly and reliably it is possible.

  2. Write an HttpModule and abuse some other event like request start. Of course I would also have to keep some data somewehere that my routes have already been registered and that registration would happen only the first time round.

Are there any better ways/places to register routes?


Maybe this article will be useful to you

http://haacked.com/archive/2010/01/17/editable-routes.aspx

You could use an HTTP module to set the routes on the RouteTable.Routes RouteCollection object.


Catching Application Start event in an HttpModule is possible

If you check my other question you will see this is easily possible. I've done it afterwards and I can assure you it works.

I've also blogged about this feature... Twice:

  • Writing a custom IHttpModule that handles Application_OnStart event
  • How to correctly use IHttpModule to handle Application_OnStart event
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜