找不到方法:'System.Web.Http.ApiController.get_Request()'

问题描述 投票:0回答:4

我有一个Azure Web应用程序,在调用Web API Controller Post方法时,我遇到了以下异常:

Method not found: 'System.Net.Http.HttpRequestMessageSystem.Web.Http.ApiController.get_Request()'

在本地运行应用程序时,我无法重现此错误,它只发生在Azure上。

这是控制器的代码:

using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TFL.BL;
using TFL.BO;
using TFL.Web.DTO;

namespace TFL.WebAPI.Controllers
{
    public class SearchController : ApiController
    {
        public HttpResponseMessage Get() 
        {
            return Request.CreateResponse(200);
        }
        // POST: api/Search
        public HttpResponseMessage Post([FromBody]SearchDTO dto)
        {
            Search s = new Search();
            s.FirstName = dto.FirstName;
            s.LastName = dto.LastName;
            s.Email = dto.Email;
            s.BirthDate = dto.BirthDate;
            s.City = dto.City;
            s.Country = dto.Country;
            s.FromAge = Convert.ToInt32(dto.FromAge);
            s.Gender = Convert.ToInt32(dto.Gender);
            s.ImagePath = dto.ImagePath;
            s.PageNumber = Convert.ToInt32(dto.PageNumber);
            s.PathUrl = dto.PathUrl;
            s.ProfessionID = Convert.ToInt32(dto.ProfessionID);

            s.State = dto.State;
            s.Str = dto.Str;
            s.Tags = null;
            s.ToAge = Convert.ToInt32(dto.ToAge);
            s.TypeID = Convert.ToInt32(dto.TypeID);
            s.Culture = dto.Culture;

            return Request.CreateResponse(HttpStatusCode.Accepted, SearchBL.GetList(s));
        }

        // PUT: api/Search/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Search/5
        public void Delete(int id)
        {
        }
    }
}

堆栈跟踪:

at TFL.WebAPI.Controllers.SearchController.Post(SearchDTO dto)
↵   at lambda_method(Closure , Object , Object[] )
↵   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
↵   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
↵   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
↵--- 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.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.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.Controllers.ActionFilterResult.<ExecuteAsync>d__2.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.<SendAsync>d__1.MoveNext()

在本地运行应用程序时,我无法重现此错误,它只会发生在Appreciate任何帮助给出!

c# azure asp.net-web-api
4个回答
0
投票

我测试了你的代码,它在我的Azure Web App上运行良好。由于它在您的开发机器上运行良好,请检查以下选项以在发布之前删除其他文件。

enter image description here


0
投票

我设法通过将我的Web API函数的返回类型更改为实际对象而不是HttpResponseMessages来解决此问题。奇迹般有效。


0
投票

我最近遇到了类似的问题。我不得不改变

return ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new Error(ModelState, 0)));

return BadRequest(ModelState);

我仍然不知道为什么以前的代码(在其他API项目中工作)在这里混乱,但这对我来说是修复。


0
投票

我设法通过返回一个实际的对象而不是HttpResponseMessage来解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.