UrlRewriter + HttpModule + Session Problem

I need to write a custom “UrlRewriter” using the HttpModule, at the time of “rewriting” I need access to the session and followed the advice from another SO thread:

Can I access session state from an HTTPModule?

Everything works, except for the RewritePath / Redirect part. I have no exceptions, but the browser loads forever. Is this really the best way to create urlrewriter like this?

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

    }

}
+3
source share
5 answers

I think I know what it is. Your module runs on ALL requests and assigns a handler that throws an error if there is no valid campaign code (where the rewrite / redirect occurs).

URL- " ", , , , , , ... , , ;)

:

  • Fiddler
  • app.Context.Response.Redirect - ,
  • MyHttpHandler.ProcessRequest - ,
+4

URL, - . URL BeginRequest URL- URL-. mach, HttpContext.RewritePath URL.

, , .

, Response.Redirect Context.RewritePath. Redirect , URL-. , ? URL- . , , , 404- , .

IIS 404 , , Custom404.aspx, . URL-, , URL-. , Response.Status "301 Moved Permanentently" "" URL- . URL , 404 .

, , Response.Redirect, URL- . Context.RewritePath , .

+2

URL- , ? , , ... URL, , , 404 , , (-, , ), , .aspx.

+1

, :

if (code != null)
{
    //app.Context.RewritePath(code.CampaignCodePath.Path, false);
    app.Context.Response.Redirect(code.CampaignCodePath.Path, true);
}

if , .

0

, reset , .

Thinking about this, maybe why the page loads forever! :)

0
source

Source: https://habr.com/ru/post/1702620/


All Articles