1

我一直在关注不同的教程,但它们都返回 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 页面(sharepoint)A 的 HTTPContext。

4

0 回答 0