我需要使用HttpModule编写一个自定义的“UrlRewriter”,在“重写”的那一刻我需要访问Session并遵循另一个SO线程的建议: Can I access session state from an HTTPModule? 一切正常,RewritePath / Redirect部分
Can I access session state from an HTTPModule?
一切正常,RewritePath / Redirect部分除外.我没有任何例外,但浏览器需要永远加载.这真的是构建像这样的urlrewriter的最好方法吗?
using System; using System.Web; using System.Web.SessionState; using System.Diagnostics; namespace MyCompany.Campaigns { public class CampaignRewriteModule : IHttpModule { public void Init(HttpApplication application) { application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState); application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler); } void Application_PostMapRequestHandler(object source, EventArgs e) { HttpApplication app = (HttpApplication)source; if (app.Context.Handler is IReadOnlySessionState || app.Context.Handler is IRequiresSessionState) { return; } app.Context.Handler = new MyHttpHandler(app.Context.Handler); } void Application_PostAcquireRequestState(object source, EventArgs e) { HttpApplication app = (HttpApplication)source; MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler; if (resourceHttpHandler != null) { HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler; } Debug.Assert(app.Session != null); string path = HttpUtils.Path(); if (!CampaignCodeMethods.IsValidCampaignCode(path)) return; string domain = HttpUtils.Domain(); CampaignCode code = CampaignManager.RegisterCode(path, domain.Equals(Config.Instance.Domain.ToLower()) ? null : domain); if (code != null) { //app.Context.RewritePath(code.CampaignCodePath.Path, false); app.Context.Response.Redirect(code.CampaignCodePath.Path, true); } } public void Dispose() { } public class MyHttpHandler : IHttpHandler, IRequiresSessionState { internal readonly IHttpHandler OriginalHandler; public MyHttpHandler(IHttpHandler originalHandler) { OriginalHandler = originalHandler; } public void ProcessRequest(HttpContext context) { throw new InvalidOperationException("MyHttpHandler cannot process requests."); } public bool IsReusable { get { return false; } } } } }我想我知道它是什么.您的模块在所有请求上执行,并分配一个抛出错误的处理程序,除非有一个有效的活动代码(发生重写/重定向).
但是因为这不仅仅是因为你的“处理程序广告系列代码”网址导致错误被抛出,这导致您被重定向到错误页面,该错误页面被模块捕获,该模块正在分配处理程序,抛出错误,重定向…我想你到了我要去的地方;)
否则我会尝试一些事情:
>设置Fiddler并检查无限重定向循环>在app.Context.Response.Redirect上放置一个断点 – 确保你不在无限循环中>在MyHttpHandler.ProcessRequest上放置一个断点 – 确保它没有被调用并吞下异常