我有一个FakeHttpContext我一直在尝试修改以包含一些标题用于测试目的
public static HttpContext FakeHttpContext() { var httpRequest = new HttpRequest("","http://stackoverflow/",""); var stringWriter = new StringWriter(); var httpResponse = new HttpResponse(stringWriter); var httpContext = new HttpContext(httpRequest,httpResponse); 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; }
这可以在没有标题的情况下工作,但是当我在httpRequest和stringWriter行之间添加任何这些代码行时.
httpRequest.Headers.Add("blah","1234"); httpRequest.Headers["blah"] = "1234";
它抛出
An exception of type ‘System.PlatformNotSupportedException’ occurred
in System.Web.dll but was not handled in user code