错误“使用自定义TextWriter时OutputStream不可用” /模拟httpResponse.OutputStream

时间:2018-08-29 05:51:06

标签: c# httpcontext

我需要检查来自端点的CSV导出。 我像这样创建了一个伪HttpContext对象

public static HttpContext FakeHttpContext(long systemTaskId)
{
    var httpRequest = new HttpRequest("Sales Report.csv", 
                           "http://localhost:90/a/Reports/sales/ExportReportToCsv.csv", 
                           "taskId=" + systemTaskId);

    var httpResponse = new HttpResponse(new StringWriter());
    var httpContext = new HttpContext(httpRequest, httpResponse);

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

    SessionStateUtility.AddHttpSessionStateToContext(httpContext, sessionContainer);

    return httpContext;
}

但是,当访问HttpContext.Current.Response.OutputStream时,它返回:

  

使用自定义TextWriter时,OutputStream不可用。

我需要在OutputStream中导出HttpResponse进行CSV测试。有谁知道我如何使用非自定义默认TextWriter?还是设置OutputStream?

此问题中的解决方案不适用,似乎有人遇到相同的错误,但似乎不需要创建虚拟HttpContext对象: OutputStream is not available when a custom TextWriter is used

0 个答案:

没有答案