Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 alternatives 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?

like image 300
Robert Koritnik Avatar asked Jul 27 '10 09:07

Robert Koritnik


2 Answers

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.

like image 75
amarsuperstar Avatar answered Nov 06 '22 13:11

amarsuperstar


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
like image 27
Robert Koritnik Avatar answered Nov 06 '22 11:11

Robert Koritnik