自定义 C# HttpModule 无限重定向循环

发布于 2024-08-04 07:32:29 字数 847 浏览 3 评论 0原文

我正在编写一个自定义 c# HttpModule,它将处理来自所有文件类型的请求。作为一个简单的概念证明,我通过添加对 Web 配置的 httpModules 部分的引用来设置该模块,并通过对 aspnet_isapi.dll 的引用为演示 IIS 网站添加应用程序扩展,以便它当前仅拦截“. htm” 文件

但即使“OnBeginRequest”事件中没有重要代码(代码如下),它也会导致无限重定向循环。我在 XP 上使用 IIS 5 有人有什么想法吗?

到目前为止,我只看到了与 ASPX 文件一起使用的 HttpModule 示例,但您肯定可以为任何文件类型进行配置吗?

#region IHttpModule Members

        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(OnBeginRequest);
        }

        /// 
        ///
        /// 
        /// 
        public void OnBeginRequest(Object s, EventArgs e)
        {
            HttpApplication context = s as HttpApplication;

            Uri currentURL = context.Request.Url;
            string pageName = currentURL.Segments.Last().ToLower();
        }
#endregion

I am writing a custom c# HttpModule that will handle requests from all file types. As a simple proof of concept I have setup the module by adding a reference to the httpModules section of the web config and added application extensions for the demo IIS website with a reference to the aspnet_isapi.dll so that it currently only intercepts request for ".htm" files

But even if there is no significant code in the "OnBeginRequest" event (code below) it causes an infinite redirect loop. I am using IIS 5 on XP Anyone got any ideas?

So far I have only seen HttpModule examples for use with ASPX files but surely you can configure the for any file type?

#region IHttpModule Members

        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(OnBeginRequest);
        }

        /// 
        ///
        /// 
        /// 
        public void OnBeginRequest(Object s, EventArgs e)
        {
            HttpApplication context = s as HttpApplication;

            Uri currentURL = context.Request.Url;
            string pageName = currentURL.Segments.Last().ToLower();
        }
#endregion

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

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

发布评论

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

评论(1

彼岸花似海 2024-08-11 07:32:29

好的。问题实际上出在 HttpModule 本身。

看来您必须使用 HttpApplication 上下文才能在客户端上呈现。

例如,在执行完所有自定义逻辑后,您需要写入上下文:

context.Response.Write("/n/r");

//or

context.Response.Redirect("test.htm");

然后一切都会按照您的预期呈现

OK. The problem was actually in the HttpModule itself.

It appears that you have to use the HttpApplication context in order for it to render on the client.

For Example after you have performed all your custom logic you need to write to the context:

context.Response.Write("/n/r");

//or

context.Response.Redirect("test.htm");

Everything then renders as you would expect

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