我正在我的 Web API 2 应用程序中编写一些身份验证逻辑,并且在主题行中出现错误。起初我只是返回 Unauthorized() 但后来我在主题行中得到了错误。当我重新访问代码时,我注意到 Intellisense 没有显示 Unauthorized 的无参数构造函数,所以我不确定为什么它没有触发构建错误。看起来构造函数只接受 AuthenticationHeaderValues 的集合。因此,我更新了我的实现,只为这个参数传递了 null,但我仍然在主题行中收到错误。
我没有收到编译器或运行时错误,说参数丢失或需要,只是找不到方法,这看起来很奇怪。知道问题可能是什么吗?您能否提供一个基本的工作示例,其中包含我可以传递的参数,或者这似乎是一个不同的问题?
更新
我按照以下答案中的说明进行操作,但仍然遇到相同的错误。这是完整的堆栈跟踪:
"{\"Message\":\"An error has occurred.\",\"ExceptionMessage\":\"Method not found: 'System.Web.Http.Results.UnauthorizedResult System.Web.Http.ApiController.Unauthorized(System.Collections.Generic.IEnumerable`1<System.Net.Http.Headers.AuthenticationHeaderValue>)'.\",\"ExceptionType\":\"System.MissingMethodException\",\"StackTrace\":\" at SecurityApi.Controllers.AuthenticationController.<Authenticate>d__3.MoveNext()\\r\\n at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)\\r\\n at SecurityApi.Controllers.AuthenticationController.Authenticate(UserCredentials userCredentials)\\r\\n at lambda_method(Closure , Object , Object[] )\\r\\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass12.<GetExecutor>b__8(Object instance, Object[] methodParameters)\\r\\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\\r\\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()\"}"