使用XML调用Web API时出现500错误 [英] 500 error while calling Web API with XML

查看:68
本文介绍了使用XML调用Web API时出现500错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I developed a web api in .net 4.5. When I test the api call, I am getting a 500 error. This behavior only happens when I send in XML. If I use JSON of Form URL encoded string the server handles the request properly.

我添加了

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

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

to the WebApiConfig.cs and the error returned is:

<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,并且没有任何内容写入该日志):

The WebAPI code looks like this(log is using log4net, and nothing is written to that log):

    /// <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);

    }

有人对为什么会引发此错误有任何想法吗?

Does anyone have any ideas on why this error is being thrown?

谢谢,史蒂文

推荐答案

此问题通过确保XML正确来解决.为此,我创建了一个新的Web api函数,该函数具有我想要的返回类型并填充了我想要的字段,然后使用确切的响应将其发送到Web api中.

This was resolved by ensuring that that the XML is correct. To do this I created a new web api function that had the return type I wanted and with the fields I wanted filled in, and used that exact response to send into the web api.

这篇关于使用XML调用Web API时出现500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆