【问题标题】:500 error while calling Web API with XML使用 XML 调用 Web API 时出现 500 错误
【发布时间】:2014-04-02 18:46:37
【问题描述】:

我在 .net 4.5 中开发了一个 web api。当我测试 api 调用时,我收到 500 错误。此行为仅在我发送 XML 时发生。如果我使用表单 URL 编码字符串的 JSON,服务器会正​​确处理请求。

我加了

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

到WebApiConfig.cs,返回的错误是:

<Error><Message>An error has occurred.</Message><ExceptionMessage>Object reference not set to an instance of an object.</ExceptionMessage><ExceptionType>System.NullReferenceException</ExceptionType><StackTrace>   at System.Web.Http.ApiController.&lt;InvokeActionWithExceptionFilters&gt;d__1.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.&lt;SendAsync&gt;d__0.MoveNext()</StackTrace></Error>

我的标题是:

User-Agent: Fiddler
Content-Type: application/xml; charset=utf-8
Host: corebeta.pexcard.com
Content-Length: 119

我的要求是:

<Admin.CardListRequest>
<username>TestUser</username>
<password>GoodPassword</password>
</Admin.CardListRequest>

我的班级是这样的:

public class CardListRequest{
    public string UserName { get; set; }
    public string Password { get; set; }
}

WebAPI 代码如下所示(日志使用 log4net,没有任何内容写入该日志):

    /// <summary>
    /// Gets the list of open cardholders
    /// </summary>
    /// <param name="UserName">The username you use to log on to pexcardadmin.com</param>
    /// <param name="Password">The password you use to log on to pexcardadmin.com</param>
    /// <returns>A list of cardholders with balances</returns>
    [HttpGet]
    public List<Admin.Account> CardList(string UserName, string Password)
    {
        try
        {

            log.Info(String.Format("Admin Parameters: <UserName={0}>", UserName));
            AuthenticateApi(UserName, Password, "cardList");
            var Ret = GetCoreCardCardList(UserName, Password);
            APIData.Response = "Success";


            return Ret;
        }
        catch (Exception e)
        {
            log.Error(e);
            APIData.Response = e.Message;
            throw e;
        }
        finally
        {
            APIData.ResponseTime = DateTime.Now;
            APILog.LogAPI(APIData);
        }

    }
    /// <summary>
    /// Gets the list of open cardholders
    /// </summary>
    /// <param name="Req"> The Request Parameter</param>
    /// <returns>A list of cardholders with balances</returns>
    [HttpPost]
    public List<Admin.Account> CardList(Admin.CardListRequest Req)
    {
        APIData.ParamBlob = JsonConvert.SerializeObject(Req);
        return CardList(Req.UserName, Req.Password);

    }

有没有人知道为什么会抛出这个错误?

谢谢, 史蒂文

【问题讨论】:

  • 显示处理请求的 web api 代码
  • 我已将我的代码添加到原始帖子中。

标签: c# .net xml asp.net-web-api


【解决方案1】:

已通过确保 XML 正确来解决此问题。为此,我创建了一个新的 web api 函数,它具有我想要的返回类型和我想要填写的字段,并使用该确切响应发送到 web api。

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 1970-01-01
    • 2020-06-17
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多