简体   繁体   English

IHttpHandler实现

[英]IHttpHandler implementation

so I am trying to create a situation that allows us to test two versions of a portion of our application randomly. 因此,我正在尝试创建一种情况,允许我们随机测试应用程序一部分的两个版本。 I've created an implementation of the IHttpHandler (C#, asp.net webforms 4, website project) and when a request comes in for our 'shop', I evaluate whether or not they should see shop or shopB. 我已经创建了IHttpHandler的实现(C#,asp.net webforms 4,网站项目),当对我们的“商店”提出要求时,我评估他们是否应该看到shop或shopB。

Here's the implementation of the handler: 这是处理程序的实现:

public class ShopRequestHandler : IHttpHandler, IReadOnlySessionState
    {
        public ShopRequestHandler() { }

        public void ProcessRequest(HttpContext context)
        {
            var originalRequest = context.Request.RawUrl;
            var process = originalRequest.Contains("shop");
            if (process)
            {
                if (UsingShopB(context))
                {
                    var newRequest = originalRequest.Replace("shop", "shopB");
                    context.Response.Redirect(newRequest, false);
                }
                else
                {
                    context.Response.Redirect(originalRequest, false);
                }
            }

        }
        public bool IsReusable
        {
            get { return false; }
        }

        private bool UsingShopB(HttpContext context)
        {
            HttpCookie cookie = context.Request.Cookies["UseShopB"];
            if (cookie == null)
            {
                var rand = new Random();
                var val = rand.Next(1, 10);
                context.Response.AppendCookie(val%2 == 0
                    ? new HttpCookie("UseShopB", true.ToString())
                    : new HttpCookie("UseShopB", false.ToString()));
            }
            var httpCookie = context.Response.Cookies["UseShopB"];
            return httpCookie != null && Boolean.Parse(httpCookie.Value);
        }
    }

The problem is not with redirecting to shop b, that works great. 问题不在于重定向到商店b,效果很好。 It's really just when I want the regular version of shop. 只是当我想要普通版的商店时。 I get a crazy request loop and an error saying that the page cannot be displayed. 我收到一个疯狂的请求循环,并显示一条错误消息,指出无法显示该页面。 It seems to me that I'm not really doing anything different when shop is requested. 在我看来,请求购物时我并没有做任何不同的事情。 It's when shopB is requested that modifications happen. 要求shopB进行修改。 Stumped. 难住了。 Should I not be using RawUrl? 我不应该使用RawUrl吗? What am I missing? 我想念什么? This is a production problem so any help would be huge. 这是一个生产问题,因此任何帮助都是巨大的。 and just to re-iterate, when the random choice is shopB it works perfectly. 再重申一次,当随机选择是shopB时,它的效果很好。 grrr. 哎呀。

I just wanted to add that when I modify the path everything works fine, what I need help with is how to leave the original request alone when I don't need to modify anything. 我只是想补充一点,当我修改路径时,一切正常,我需要帮助的是如何在不需要修改任何内容时将原始请求保留下来。 Help! 救命!

Thank you -Sean 谢谢-塞恩

The problem is with the check you've placed for Shop: 问题在于您为商店存入的支票:

var process = originalRequest.Contains("shop") var process = originalRequest.Contains(“ shop”)

This will be true for both "Shop" and "ShopB" so in case of Shop it will return "ShopB" but in case of ShopB it'll returned "ShopBB" “ Shop”和“ ShopB”均适用,因此如果使用Shop,它将返回“ ShopB”,但是如果使用ShopB,则将返回“ ShopBB”

you need more refined check for this filter. 您需要对此过滤器进行更精细的检查。 Either reverse the check or use exact match with word count. 要么取消检查,要么使用完全匹配的字数统计功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM