0

我从控制器调用了一个 HttpPost 方法到 Web API。对于其他方法,响应正常并且该方法运行良好,除了下面提到的方法说明错误

{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  Connection: Close
  Cache-Control: no-cache
  Date: Fri, 16 Aug 2013 09:49:25 GMT
  Server: ASP.NET
  Server: Development
  Server: Server/10.0.0.0
  X-AspNet-Version: 4.0.30319
  Content-Length: 1855
  Content-Type: application/json; charset=utf-8
  Expires: -1
}}

我的方法

public int CustomerRegistration(CustomerRequestResponse req)
      {
                try
                {
                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ServerAddress"].ToString());
                    client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                    Uri gizmoUri = null;
                    var Response = client.PostAsJsonAsync("api/MyAccount/CustomerRegistration", req).Result;
                    int result = 0;
                    if (Response.IsSuccessStatusCode)
                    {
                        gizmoUri = Response.Headers.Location;
                        result = Response.Content.ReadAsAsync<Int32>().Result;
                    }
                    return result;
                }
                catch { throw; }
            }

我如何确定我的代码中的错误在哪里。有什么建议么 ??

编辑 :: 跟踪指定“System.Net.Http.ObjectContent”的错误

{Method: POST, RequestUri: 'http://localhost:56003/api/MyAccount/CustomerRegistration', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[ServiceEntities.MyAccount.CustomerRequestResponse, ServiceEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Headers:
{
  Accept: application/json
  Content-Type: application/json; charset=utf-8
  Content-Length: 495
}}

有什么修复它的建议吗?

编辑1:

</Message><ExceptionMessage>No MediaTypeFormatter is available to read an object of type 'CustomerRequestResponse' from content with media type 'text/plain'.</ExceptionMessage>
4

3 回答 3

2

我要做的第一件事是验证客户端所做的请求是否正确——正确的 url、headers、body。为此,您可以fiddler在应用程序中启用代理。将此部分添加到您的Web.config并尝试在 fiddler 打开的情况下再次运行代码 - 现在您应该能够在 fiddler 中看到请求:

<system.net>
  <defaultProxy>
    <proxy  proxyaddress="http://127.0.0.1:8888" />      
  </defaultProxy>
</system.net>

如果请求正常,请检查您尝试连接的服务器是否实际启动并正在运行,并且它是否为您提供了带有正确标头的正确响应。

我怀疑它应该足以识别问题。

顺便说一句:在现实生活中,你的catch积木真的有用吗?为了使它比现在更有用,您可以添加一些 log4net 日志记录,这可能也有助于识别任何未来的问题。

于 2013-08-16T18:48:25.807 回答
0

我认为在处理 MyAccountController 的 CustomerRegistration 操作数据时存在错误。如果在进行过程中发生错误,我希望您检查您的控制器。

于 2013-08-17T06:26:27.813 回答
0

如果您有标准的 DLL(后端)和 Web Script(前端:如 ashx 或 aspx 等)设置,那么很容易忘记上传(到服务器)对 Web Script 所做的更改。我收到了这样的错误,但是当我确保我的所有代码/脚本都同步并上传到服务器时,它就消失了。

于 2016-08-25T06:43:32.003 回答