内容协商期间HttpControllerContext

内容协商期间HttpControllerContext

本文介绍了内容协商期间HttpControllerContext.Configuration有时为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在设置ASP.NET 4.6.2 Web Api主机,并注意到某些请求由于以下错误而失败:

We are setting up a ASP.NET 4.6.2 Web Api host and noticing that some requests fail because of the following error:

HttpControllerContext.Configuration must not be null.

我一直无法重现问题,想问问是否有人可以引导我朝正确的方向前进?

I've been unable to reproduce the issue and wanted to ask if anybody could steer me in the right direction?

这是堆栈跟踪:


[0] System.InvalidOperationException "HttpControllerContext.Configuration must not be null."
   at System.Web.Http.Results.NegotiatedContentResult`1.ApiControllerDependencyProvider.EnsureResolved()
   at System.Web.Http.Results.NegotiatedContentResult`1.ApiControllerDependencyProvider.get_ContentNegotiator()
   at System.Web.Http.Results.BadRequestErrorMessageResult.Execute()
   at System.Web.Http.Results.BadRequestErrorMessageResult.ExecuteAsync(CancellationToken cancellationToken)
   at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()

推荐答案

将标头添加到BadRequestErrorMessageResult时,我的测试中发生了类似的错误.该修补程序仅用于初始化控制器的配置....然后初始化请求.

A similar error occurred in my tests when adding headers to a BadRequestErrorMessageResult. The fix only required to initialize the controller's Configuration....and subsequently initialize the Request.

所以我的测试设置最终看起来像

So my test's setup ended up looking something like

public class FooControllerTest
{
    private FooController _sut;

    [Setup]
    public void Setup()
    {
        _sut = new FooController();
        _sut.Configuration = new System.Web.Http.HttpConfiguration();
        _sut.Request = new System.Net.Http.HttpRequestMessage();
    }
}

希望有帮助.

这篇关于内容协商期间HttpControllerContext.Configuration有时为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:31