伪造httpcontext返回null

时间:2012-07-11 08:20:18

标签: c# asp.net http web sitemap

我一直在关注不同的教程,但所有这些教程都返回null,我一直在使用的代码是,

  public HttpContext SetNewHttpContext(string uRL)
    {
        var httpRequest = new HttpRequest("", uRL, "");
        var httpResponse = new HttpResponse(new StringWriter());
        return new HttpContext(httpRequest, httpResponse);
    }

我也试过

        public static HttpContext FakeHttpContext(string uRL)
    {
        var httpRequest = new HttpRequest("", uRL, "");
        var stringWriter = new StringWriter();
        var httpResponce = new HttpResponse(stringWriter);
        var httpContext = new HttpContext(httpRequest, httpResponce);

        var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
                                                new HttpStaticObjectsCollection(), 10, true,
                                                HttpCookieMode.AutoDetect,
                                                SessionStateMode.InProc, false);

        httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
                                    BindingFlags.NonPublic | BindingFlags.Instance,
                                    null, CallingConventions.Standard,
                                    new[] { typeof(HttpSessionStateContainer) },
                                    null)
                            .Invoke(new object[] { sessionContainer });

        return httpContext;
    }

但它们实际上没有用,我用

调用这个上下文
 SiteMapProvider contentMapProvider = SiteMap.Providers["SPContentMapProvider"];
 SiteMapNode currentNode = contentMapProvider.FindSiteMapNode(FakeHttpContext(Request.UrlReferrer.ToString()));

它给了我错误

  

由于对象的当前状态,操作无效。

方案

当我们从页面A导航到页面B时,我需要另一个ASP.NET页面B中的不同ASP.NET页面(共享点)A的HTTPContext。

0 个答案:

没有答案