ASP.NET MVC Web API:发布对象列表

3

我正在尝试从我的WinForms应用程序将对象列表发布到我的ASP.NET MVC 4网站。我已经测试了发布一个对象,它可以工作,但是对于列表则不行。它返回一个500(内部服务器错误)。这是我的代码:

ASP.NET MVC Web API

public class PostTraceController : ApiController
{
    public HttpResponseMessage Post(List<WebTrace> list)
    {
        try
        {
            // Some code
            return Request.CreateResponse(HttpStatusCode.Created);
        }
        catch (Exception ex)
        {
            HttpContext.Current.Trace.Write("exception", ex.Message);
            return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, ex);
        }
    }

    public HttpResponseMessage Post(WebTrace item)
    {
        try
        {
            // Some code
            return Request.CreateResponse(HttpStatusCode.Created);
        }
        catch (Exception ex)
        {
            HttpContext.Current.Trace.Write("exception", ex.Message);
            return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, ex);
        }
    }
}

Win forms application

public class BaseSender
{
    public BaseSender()
    {
        Client = new HttpClient
        {
            BaseAddress = new Uri(@"http://localhost/mywebsite/")
        };

        Client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
    }

    public string UserCode { get; set; }
    protected readonly HttpClient Client;

    public HttpResponseMessage PostAsJsonAsync(string requestUri, object value)
    {
        var response = Client.PostAsJsonAsync(requestUri, value).Result;
        response.EnsureSuccessStatusCode();
        return response;
    }
}

public class WebTraceSender : BaseSender
{
    private const string requestUri = "api/posttrace";

    public bool Post(List<ArchiveCptTrace> list)
    {
        try
        {
            var listWebTrace = new List<WebTrace>();
            foreach (var item in list)
            {
                listWebTrace.Add(new WebTrace
                    {
                        DateStart = item.DatePreparation,
                        DateEnd = item.DateCloture,
                        UserStart = item.UserPreparation.UserName,
                        UserEnd = item.UserCloture.UserName,
                        AmountStart = item.MontantPreparation,
                        AmountEnd = item.MontantCloture,
                        TheoricAmountEnd = item.MontantTheorique,
                        Difference = item.Ecart,
                        UserCode = UserCode
                    });
            }

            var responce = PostAsJsonAsync(requestUri, listWebTrace);
            return responce.IsSuccessStatusCode;
        }
        catch (Exception e)
        {
            // TODO : Trace the exception
            return false;
        }
    }
}

编辑:

我已经找出了错误的场景,即我的api控制器中有两种方法,尽管它们具有不同的签名。如果我注释掉一个方法,那么发布就可以正常工作(项目或列表)。有什么想法吗?


您在500响应中看到错误消息吗?如果是这样,能否分享一下是什么错误消息? - Kiran
@KiranChalla:不好意思,没有任何消息,即使在跟踪页面上(localhost/mywebsite/trace.axd)。 - SidAhmed
1个回答

2

这些方法的签名可能不同,但是由于性能原因,Web API无法检查它们的区别,除非检查其中的内容。

你可以做两件事情 - 要么创建一个新类,只包含WebTrace对象列表,并将其放入不同的API控制器中,要么将自定义路由映射到现有方法之一。你可以使用ActionName属性来实现,但我可能会采用第一种方法。


1
+1 对于你的解释,以及你的回答非常有帮助。虽然第一种方法似乎很诱人,但出于可维护性的原因,我选择了路由方法,更具体地说,是属性路由(http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2)。再次感谢 :-) - SidAhmed
是的,属性路由现在是首选,而不是ActionName属性。谢谢,我会更新答案。 - Greg Ennis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接