Method not found: then send request web api controller

Posted

技术标签:

【中文标题】Method not found: then send request web api controller【英文标题】:Method not found: when send request web api controler 【发布时间】:2018-12-30 01:11:32 【问题描述】:

当我尝试调用 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>

【问题讨论】:

【参考方案1】:

作为替代解决方案,您可以使用属性路由 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】:

Get 方法的名称不正确将名称从“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);
        

【讨论】:

感谢您的建议。但不适合我!如果有任何不同的方法,请提出建议。 能否添加整个 API 控制器代码 sn-p 且不包含核心业务逻辑 我尝试了以下测试方法,但它也不起作用。这是代码详细信息:- [System.Web.Http.HttpGet] public HttpResponseMessage GetTest() var res = Request.CreateResponse(HttpStatusCode.OK);字符串 json = JsonConvert.SerializeObject("ABC"); res.Content = new StringContent(json, Encoding.UTF8, "application/json");返回资源; 你是怎么从浏览器调用这个的?【参考方案3】:

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

【讨论】:

以上是关于Method not found: then send request web api controller的主要内容,如果未能解决你的问题,请参考以下文章

Gradle DSL method not found: 'android()

Error: Main method not found in class

解决Gradle DSL method not found: ‘android()’

Gradle DSL method not found: 'google()'

Gradle DSL method not found: 'google()'

升级EF导致的错误---MissingMethodException: Method not found: ...IPropertyBase.get_Name()