更新时间:2023-02-17 08:25:08
另一种解决此问题的方法是执行以下操作:
Another way to solve this is to do the following:
controller.Request = new HttpRequestMessage();
controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey,
new HttpConfiguration());
如果您要升级到 webapi 5.0,则需要将其更改为:
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());
您需要这样做的原因是因为您必须在控制器上填充 Request
否则 Request
上的扩展方法将不起作用.您还必须在请求上设置 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.