Another way to solve this is to do the following:
controller.Request = new HttpRequestMessage();
controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey,
new HttpConfiguration());
If you are upgrading to webapi 5.0, then you'll need to change this to:
controller.Request = new HttpRequestMessage();
controller.Request.SetConfiguration(new HttpConfiguration());
The reason why you need to do this is because you have to have Request
populated on the controller otherwise the extension methods on Request
won't work. You also have to have an HttpConfiguration
set on the Request otherwise routing and other parts of the pipeline won't function correctly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…