IRouteHandler in Web Forms: Routing requests that require HttpContext.User

时间秒杀一切 提交于 2019-12-08 01:28:22

问题


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!


回答1:


See the answer to this question, very similar.




回答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.



来源:https://stackoverflow.com/questions/1534162/iroutehandler-in-web-forms-routing-requests-that-require-httpcontext-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!