BaseHttpContext
実際のリクエストを行わずに、いくつかの(モック)テストを行うためのダミーを作成しようとしています。ダミーコンテキストにIPアドレスを追加するのが好きです。
string url = "http://www.google.com";
//wrap in uri
Uri uri = new Uri(url);
//create request
var request = new HttpRequest("", uri.GetLeftPart(UriPartial.Path), uri.Query);
request.ServerVariables.Add("REMOTE_ADDR", ip);
//dummy context
HttpContext context = new HttpContext(
new HttpRequest("", uri.GetLeftPart(UriPartial.Path), uri.Query),
new HttpResponse(new StringWriter(System.Text.Encoding.UTF8))
);
//wrap context
var contextWrapper = new HttpContextWrapper(context);
IPはに反映されている必要がありますBaseHttpContext.Request.UserHostAddress
。次のようなIPを追加するrequest.ServerVariables.Add("REMOTE_ADDR", ip);
と、次の例外が発生します。
指定された方法はサポートされていません。