Web API在本地上工作正常,但在服务器上返回501?

时间:2018-08-31 05:08:56

标签: asp.net asp.net-mvc asp.net-web-api asp.net-web-api2

我正在使用WebAPI2。这是我的控制器类:

public class ValuesController : ApiController
{     
    [HttpPost]
    public HttpResponseMessage LoginMethod()
    {      
     HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "This is a simple message");
     return response;
    }
}

我可以从邮递员本地测试此方法,它可以为我提供适当的输出,但在服务器上失败。此方法位于ValuesController内部,我通过这种方式(在本地)调用

http://localhost:49476/api/values/LoginMethod/

,它运行正常。在服务器上,我通过这种方式致电

http://example.com/api/values/LoginMethod/

它给我错误,状态代码为500 internal server error

 {
  "Message": "An error has occurred.",
  "ExceptionMessage": "Value cannot be null.\r\nParameter name: nameValueCollection",
  "ExceptionType": "System.ArgumentNullException",
  "StackTrace": "   at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__1.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__0.MoveNext()"
 }

这是我的WebAPIConfig文件:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );
}

请建议我我做错了什么?任何帮助将非常感激。谢谢。

0 个答案:

没有答案