我在我的控制器中使用EF將記錄保存到數據庫中的代碼如下。System.ArgumentNullException被用戶代碼未處理
[Route("api/abc/PostValues")]
public HttpResponseMessage PostValues(p_abc p_abc)
{
if (ModelState.IsValid)
{
db.p_abc.Add(p_abc);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, p_abc);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = p_abc.p_abc_id }));
return response;
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
數據寫入數據庫罰款。
但在下面一行
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = p_abc.p_abc_id }));
我得到以下異常
System.ArgumentNullException was unhandled by user code
HResult=-2147467261
Message=Value cannot be null.
Parameter name: uriString
Source=System
ParamName=uriString
StackTrace:
at System.Uri..ctor(String uriString)
at App.Controllers.abcController.p_abc(p_abc p_abc) in c:\xxx\abcController.cs:line 127
at lambda_method(Closure , Object , Object[])
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
InnerException:
請讓我知道如何像你有沒有路由相匹配的數據修復此異常
你能確認你有你'WebApiConfig.cs'爲'DefaultApi'條目?它應該看起來像這樣:'config.Routes.MapHttpRoute(「DefaultApi」,「...' – McCee