Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未找到方法:System.Web.Http.Results.UnauthorizedResult_C#_.net_Asp.net Web Api_Webapi2 - Fatal编程技术网

C# 未找到方法:System.Web.Http.Results.UnauthorizedResult

C# 未找到方法:System.Web.Http.Results.UnauthorizedResult,c#,.net,asp.net-web-api,webapi2,C#,.net,Asp.net Web Api,Webapi2,我正在我的WebAPI2应用程序中编写一些身份验证逻辑,并且在主题行中出现了错误。起初我只是返回Unauthorized(),但后来在主题行中出现了错误。当我重温代码时,我注意到Intellisense没有为未经授权的用户显示无参数构造函数,所以我不确定为什么这不会触发生成错误。contstructor似乎只接受AuthenticationHeaderValue的集合。因此,我更新了我的实现,只为这个参数传递null,但我仍然在主题行中得到错误 我没有收到编译器或运行时错误,说参数丢失或必需,只

我正在我的WebAPI2应用程序中编写一些身份验证逻辑,并且在主题行中出现了错误。起初我只是返回Unauthorized(),但后来在主题行中出现了错误。当我重温代码时,我注意到Intellisense没有为未经授权的用户显示无参数构造函数,所以我不确定为什么这不会触发生成错误。contstructor似乎只接受AuthenticationHeaderValue的集合。因此,我更新了我的实现,只为这个参数传递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()\"}"
“{\”Message\”:\“出现错误。\”,“ExceptionMessage\”:“找不到方法:'System.Web.Http.Results.UnauthorizedResult System.Web.Http.ApiController.Unauthorized(System.Collections.Generic.IEnumerable`1).\”,“ExceptionType\”:“System.MissingMethodException\,“StackTrace\:”在SecurityApi.Controllers.AuthenticationController.d_\u 3.MoveNext()\\r\\n在System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.启动[TStateMachine](TStateMachine和stateMachine)\\r\\n在SecurityApi.Controllers.AuthenticationController.Authenticate(UserCredentials UserCredentials)\\r\\n在lambda\u方法(闭包、对象、对象[])上\\r\\n位于System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c_uDisplayClass12.b_u8(对象实例,对象[]方法参数)\\r\\n位于System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象实例,对象[]参数)\\r\\n在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2参数,CancellationToken CancellationToken)\\r\\n--从引发异常的上一个位置开始的堆栈结束跟踪---\\r\\n在System.Runtime.CompilerServices.TaskWaiter.ThrowforOnSuccess(任务任务)\\r\\n位于System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\\r\\n位于System.Web.Http.Controller.ApiControllerActionInvoker.d_u0.MoveNext()\\r\\n---从引发异常的上一个位置开始的堆栈结束跟踪---\\r\\n在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)\\r\\n在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务)\\r\\n在System.Web.Http.Controllers.ActionFilterResult.d_u2.MoveNext()\\r\\n---从引发异常的上一个位置开始的堆栈结束跟踪---\\r\\n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务任务)\\r\\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务任务)\\r\\n位于System.Web.Http.Dispatcher.HttpControllerDispatcher.d\u 1.MoveNext()\“}”

我尝试过,当我使用:

 namespace TestWebApp.Api.Controllers
 {
    using System.Web.Http;

    public class CapController : ApiController
    {
        public IHttpActionResult GetVehicle()
        {
            return this.Unauthorized();
        }
    }
}
它未经授权返回401

如果要使用AuthenticationHeaderValue:

namespace TestWebApp.Api.Controllers
{
    using System.Collections.Generic;
    using System.Net.Http.Headers;
    using System.Web.Http;

    public class CapController : ApiController
    {
        public IHttpActionResult GetVehicle()
        {
            return this.Unauthorized(new List<AuthenticationHeaderValue>()
                                         {
                                             new AuthenticationHeaderValue("Authorization")
                                         });
        }
    }
}
namespace TestWebApp.Api.Controllers
{
使用System.Collections.Generic;
使用System.Net.Http.Header;
使用System.Web.Http;
公共类CapController:ApiController
{
公共IHttpActionResult GetVehicle()
{
返回此。未经授权(新列表()
{
新的AuthenticationHeaderValue(“授权”)
});
}
}
}
它会将此标题添加到响应:

www认证→授权,持票人