Question

I'm trying to add a pretty basic route to an Asp.Net Web Forms app (running under IIS 7, integrated mode): for requests coming to http://mydomain.com/foo/ I would like to show the results of a dynamic page (http://mydomain.com/foopage.aspx).

I've created a RouteHandler that does all this and it seems to be routing correctly.

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
     var page = System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/foo.aspx", typeof(MyApp.Web.Foo)) as MyApp.Web.Foo;
     return page as IHttpHandler;
    }

The problem is, inside my RouteHandler's GetHttpHandler method, all the instances of the current user (requestContext.HttpContext.User, System.Web.HttpContext.Current.User) are null. Sadly, foo.aspx needs to know what the current user is (for login controls, role stuff, etc), so rendering the page is throwing null reference exceptions. My guess is that these route handlers are firing off before Asp.Net gets the chance to wire up the HttpContext with user info. Any idea of a work-around?

PS - I realize this can be accomplished by doing a Server.Transfer in a page at http://mydomain.com/foo/default.aspx. I'd like to use routing for this sort of thing rather than having a bunch of useless folders cluttering things up.

Thanks!

Was it helpful?

Solution 2

I managed to figure this one out myself.

Much like this question, my routes were working just fine when the route origin ended in .aspx (http://mydomain.com/foo-origin.aspx), but failed when they did not (http://mydomain.com/foo-origin/).

The MSDN article on setting up routing with web forms tells you to make a few changes to web config, but leaves out that you need to set runAllManagedModulesForAllRequests to true in the modules node:

<configuration>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
  </system.webServer>
</configuration>

Now it works swimmingly.

OTHER TIPS

See the answer to this question, very similar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top