在控制台应用程序中人工填充HttpContext对象 [英] Artificially populate HttpContext object in a Console application

查看:105
本文介绍了在控制台应用程序中人工填充HttpContext对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为log4net编写包装器库.该库应该能够捕获上下文信息,例如查询字符串,cookie,表单字段等.

I am writing a wrapper library for log4net. This library should be able to capture Context information such as querystring, cookie, form fields etc. etc.

我正在从Console应用程序调用此包装器类,而不是TDD类.

I am invoking this wrapper class from Console application as opposed to TDD class.

是否有如下方法可以在控制台应用程序内填充HttpContext对象?

Is there a way to populate HttpContext object inside a Console application as follows?

HttpContext c = new HttpContext(null);
c.Request.QueryString.Keys[1] = "city";
c.Request.QueryString[1] = "Los Angeles";
c.Request.QueryString.Keys[2] = "state";
c.Request.QueryString[2] = "CA";

然后按以下方式检索它?

And then retrieve it as follows?

Console.WriteLine(context.Request.QueryString.Keys[1]);

尝试上面的代码时出现以下异常.

I am getting the following exception when I attempt the above code.

Property or indexer 'System.Collections.Specialized.NameObjectCollectionBase.KeysCollection.this[int]' cannot be assigned to -- it is read only 

推荐答案

您可以在此处阅读有关使用Moq进行伪造的信息: http://o2platform.wordpress.com/2011/04/05/mocking-httpcontext-httprequest-and-httpresponse-for-unittests-using-moq/

you can read here on using Moq to fake it out: http://o2platform.wordpress.com/2011/04/05/mocking-httpcontext-httprequest-and-httpresponse-for-unittests-using-moq/

这篇关于在控制台应用程序中人工填充HttpContext对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆