需要路由请求HttpContext

需要路由请求HttpContext

本文介绍了IRouteHandler在Web窗体:需要路由请求HttpContext.User中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加到一个Asp.Net Web窗体应用程序一个pretty基本路径(IIS 7集成模式下运行)的请求来的我想显示动态页面的结果(的)。

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).

我创建了一个RouteHandler,做了这一切,它似乎是正确的路由。

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;
    }

问题是,我RouteHandler的GetHttpHandler方法中,当前用户(requestContext.HttpContext.User,System.Web.HttpContext.Current.User)的所有实例都为空。可悲的是,foo.aspx需要知道当前用户是什么(用于登录控件,角色的东西,等等),所以呈现页面抛出空引用异常。我的猜测是,这些途径处理程序发射了Asp.Net得到接线与用户信息的HttpContext的机会了。一个变通的任何想法?

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 - 我知道这可以通过在。我想使用的路由为这样的事情而​​不是一堆无用的文件夹混乱的事情了。

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.

谢谢!

推荐答案

我设法弄清楚这一个我自己。

I managed to figure this one out myself.

就像this问题,当路由源中的.aspx结束了我的路线是工作得很好(),但是,当他们没有失败()。

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/).

告诉你的MSDN文章做出web配置了一些变化,但遗漏了,你需要将 runAllManagedModulesForAllRequests 以真正的模块中的节点:

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.

这篇关于IRouteHandler在Web窗体:需要路由请求HttpContext.User中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:22