找不到方法:发送请求时Web API控制器

时间:2018-07-22 10:44:22

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-web-api csharpcodeprovider

当我尝试调用api方法时,出现了

之类的异常
  

未找到方法:“无效System.Web.Http.Filters.HttpActionExecutedContext.set_Response(System.Net.Http.HttpResponseMessage)”。

ApiController:-

[System.Web.Http.HttpGet]
public HttpResponseMessage CheckUserAuthentication(string UserName, string Password)
{
    DomUserAuthentication objauthentication = new DomUserAuthentication();
    var res = Request.CreateResponse(HttpStatusCode.OK);
    /////Code here
    res.Content = new StringContent(json, Encoding.UTF8, "application/json");
    return res;
}

这是我的Webapi.config:-

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{Action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

错误详细信息:-

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Method not found: 'Void System.Web.Http.Filters.HttpActionExecutedContext.set_Response(System.Net.Http.HttpResponseMessage)'.
</ExceptionMessage>
<ExceptionType>System.MissingMethodException</ExceptionType>
<StackTrace>
at SalesRevUp.CrossCutting.DataAPIErrors.OnException(HttpActionExecutedContext context) at System.Web.Http.Filters.ExceptionFilterAttribute.System.Web.Http.Filters.IExceptionFilter.ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) at System.Web.Http.ApiController.<>c__DisplayClass8.<>c__DisplayClassa.<InvokeActionWithExceptionFilters>b__6(IExceptionFilter filter) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)
</StackTrace>
</Error>

screenshot

3 个答案:

答案 0 :(得分:0)

“获取方法的名称”不是正确的名称,从“ CheckUserAuthentication ”更改为“ GetCheckUserAuthentication

 public HttpResponseMessage GetCheckUserAuthentication(string UserName, string Password)
        {
            DomUserAuthentication objauthentication = new DomUserAuthentication();
            var res = Request.CreateResponse(HttpStatusCode.OK);
            ///////Code here
            res.Content = new StringContent(json, Encoding.UTF8, "application/json");
            return Request.CreateResponse(HttpStatusCode.OK);
        }

答案 1 :(得分:0)

作为替代解决方案,您可以利用属性路由 https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing

    [Route("IntermediaryData/CheckUserAuthentication")]
    [System.Web.Http.HttpGet]
    public HttpResponseMessage CheckUserAuthentication(string UserName, string Password)
    {
        DomUserAuthentication objauthentication = new DomUserAuthentication();
        var res = Request.CreateResponse(HttpStatusCode.OK);
        /////Code here
        res.Content = new StringContent(json, Encoding.UTF8, "application/json");
        return res;
    }

答案 2 :(得分:-2)

如果此问题仍然有效。我找到了解决方案。 问题出在.Net框架上。 我已经从4.6更新到4.7,问题已解决。