"值不能为空。\ r \ n参数名称:实体(ASP.NET Web API)

时间:2017-08-15 17:35:43

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

我有表TimeTable的webapi项目

这是表格

 CREATE TABLE [dbo].[TimeTable] (
    [Id]         INT            IDENTITY (1, 1) NOT NULL,
    [Company]    NVARCHAR (MAX) NULL,
    [INN]        NVARCHAR (MAX) NULL,
    [StartDay]   NVARCHAR (MAX) NULL,
    [StartPause] NVARCHAR (MAX) NULL,
    [EndDay]     NVARCHAR (MAX) NULL,
    [EndPause]   NVARCHAR (MAX) NULL,
    [Today]      DATE           NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

和班级

public partial class TimeTable
{
    public int Id { get; set; }
    public string Company { get; set; }
    public string INN { get; set; }
    public string StartDay { get; set; }
    public string StartPause { get; set; }
    public string EndDay { get; set; }
    public string EndPause { get; set; }
    public Nullable<System.DateTime> Today { get; set; }
}

以下是将数据添加到表

的控制器
 [ResponseType(typeof(TimeTable))]
    public IHttpActionResult PostStartWorkingDay(TimeTable startWorkingDay)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.TimeTables.Add(startWorkingDay);
        db.SaveChanges();


        //return Json(new { Result = "Success", Message = "Saved Successfully"}, startWorkingDay);

        return CreatedAtRoute("DefaultApi", new { id = startWorkingDay.Id }, startWorkingDay);
    }

当我从邮递员发送邮寄请求时

这是请求正文

{"Company":"123","INN":"123","StartDay":"15:8:2017T16:55:38"}

一切都好,数据正在写

我得到了回复

{
"Id": 1013,
"Company": "123",
"INN": "123",
"StartDay": "15:8:2017T16:55:38",
"StartPause": null,
"EndDay": null,
"EndPause": null,
"Today": null

}

但我也尝试使用restsharp

从xamarin应用程序请求

这是代码

var client = new RestClient("http://timetrackingbackend.azurewebsites.net/api");
        var request = new RestRequest("/StartWorkingDays", Method.POST);
        request.AddQueryParameter("Company", "12366");
        request.AddQueryParameter("INN", "12366");
        request.AddQueryParameter("StartDay", "8/14/2017T14.48");
        var response = client.Execute(request);
        string content = response.Content;

我有这个错误

{"Message":"An error has occurred.","ExceptionMessage":"Value cannot be null.\r\nParameter name: entity","ExceptionType":"System.ArgumentNullException","StackTrace":"   at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)\r\n   at System.Data.Entity.DbSet`1.Add(TEntity entity)\r\n   at trackingappbackend.Controllers.StartWorkingDaysController.PostStartWorkingDay(TimeTable startWorkingDay) in C:\\Users\\nemes\\Documents\\Visual Studio 2017\\Projects\\trackingappbackend\\trackingappbackend\\Controllers\\StartWorkingDaysController.cs:line 48\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\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.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.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.Controllers.ActionFilterResult.<ExecuteAsync>d__2.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__1.MoveNext()"}

我如何解决它,因为似乎请求是相同的?

1 个答案:

答案 0 :(得分:1)

尝试以json的形式发送数据。以下是使用HttpClient而不是RestSharp的示例(不是说RestSharp是错误的):

var timeTable = new TimeTable(){Company= "12366", 
                                INN = "12366",
                                StartDay = "8/14/2017T14.48");

var response = client.PostAsync("/StartWorkingDays",
                            new StringContent(
                                JsonConvert.SerializeObject(timeTable),
                                                Encoding.UTF8, "application/json")).Result;