我有一个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()
在本地运行应用程序时,我无法重现此错误,它只会发生 感谢给予的任何帮助!
答案 0 :(得分:0)
答案 1 :(得分:0)
我设法通过将Web API函数的返回类型更改为实际对象而不是HttpResponseMessages来解决此问题。像魅力一样。
答案 2 :(得分:0)
我最近遇到了类似的问题。我不得不改变
return ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new Error(ModelState, 0)));
到
return BadRequest(ModelState);
我仍然不知道为什么以前的代码(在其他API项目中工作)在这里混乱,但这对我来说是个修复。
答案 3 :(得分:0)
我设法通过返回一个实际的对象而不是HttpResponseMessage来解决这个问题。
答案 4 :(得分:0)
我将此添加到了web.config文件并进行了排序
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>