2017-08-15 100 views
0

我的WebAPI項目,表時間表「值不能爲空 r n參數名:實體(的ASP.NET Web API)

這裏是表

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

當我把從郵遞員post請求

這裏是請求主體

{"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 

}

但我也嘗試了使用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()"} 

我怎樣才能解決這個問題,因爲它看起來像請求是一樣的嗎?

+0

嘗試發送數據作爲JSON –

+0

但它發出像JSON?不? @JosueMartinez –

+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; 
+0

不明白 我在哪裏需要添加數據 –

+1

hm。似乎我發現如何發送請求 var client = new RestClient(「http://timetrackingbackend.azurewebsites.net/api」); var request = new RestRequest(「/ StartWorkingDays」,Method.POST); request.AddJsonBody(new {Company =「12366」,INN =「12366」,StartDay =「8/14/2017T14.48」}); var response = client.Execute(request); string content = response.Content;' –

+0

檢查已編輯的答案 –

相關問題