Web 表单中的 IRouteHandler:路由需要 HttpContext.User 的请求

发布于 2024-08-07 05:25:04 字数 1094 浏览 5 评论 0 原文

我正在尝试向 Asp.Net Web Forms 应用程序添加一个非常基本的路由(在 IIS 7 下运行,集成模式):对于传入 http://mydomain.com/foo/ 我想显示动态页面的结果 (http://mydomain.com/foopage.aspx)。

我创建了一个 RouteHandler 来完成所有这些工作,并且它似乎路由正确。

    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)均为 null。遗憾的是,foo.aspx 需要知道当前用户是什么(用于登录控件、角色内容等),因此呈现页面会引发空引用异常。我的猜测是,这些路由处理程序在 Asp.Net 有机会将 HttpContext 与用户信息连接之前就已启动。有什么解决办法吗?

PS - 我意识到这可以通过在 http://mydomain 的页面中执行 Server.Transfer 来完成。 com/foo/default.aspx。我想使用路由来处理这类事情,而不是让一堆无用的文件夹弄乱事情。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2024-08-14 05:25:04

请参阅 的答案这个问题,非常相似。

See the answer to this question, very similar.

与君绝 2024-08-14 05:25:04

我自己设法解决了这个问题。

很像 这个问题,当路由源以 .aspx 结尾时(http:// mydomain.com/foo-origin.aspx),但当他们没有这样做时失败(http:// /mydomain.com/foo-origin/)。

有关使用 Web 表单设置路由的 MSDN 文章 告诉您对 Web 配置进行了一些更改,但忽略了您需要在模块节点中将 runAllManagedModulesForAllRequests 设置为 true:

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

现在它可以顺利运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文