我在我的Web API项目中发布数据时遇到问题,我有一个操作方法和一个存储过程以及一个表,想要执行后期操作,但是当我发布数据时,我收到内部服务器错误消息fiddler我的表结构如下所示
public partial class tblImage
{
public string ImageId { get; set; }
public string ImagePath { get; set; }
public string ImageCategory { get; set; }
}
Visual Studio制作的存储过程代码如下所示
public virtual int InsertDynamicImage(string imageId, string imagePath, string imageCategory)
{
var imageIdParameter = imageId != null ?
new ObjectParameter("ImageId", imageId) :
new ObjectParameter("ImageId", typeof(string));
var imagePathParameter = imagePath != null ?
new ObjectParameter("ImagePath", imagePath) :
new ObjectParameter("ImagePath", typeof(string));
var imageCategoryParameter = imageCategory != null ?
new ObjectParameter("ImageCategory", imageCategory) :
new ObjectParameter("ImageCategory", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("InsertDynamicImage", imageIdParameter, imagePathParameter, imageCategoryParameter);
}
}
我的控制器代码如下所示
[HttpPost]
[Route("api/tblProducts/DynamicImage")]
public IHttpActionResult AddDynamic(tblImage Review)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var AltAddress = db.InsertDynamicImage(Review.ImageId, Review.ImagePath, Review.ImageCategory);
return CreatedAtRoute("DefaultApi", new { id = Review.ImageId }, Review);
}
fiddler给出的错误信息如下所示
{
"Message":"An error has occurred.",
"ExceptionMessage":"UrlHelper.Link must not return null.",
"ExceptionType":"System.InvalidOperationException",
"StackTrace":" at System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult`1.Execute()\r\n
at System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult`1.ExecuteAsync(CancellationToken cancellationToken)\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 :(得分:0)
尝试命名属性路由。
[Route("api/tblProducts/DynamicImage", name="GetProductImage")]
然后在调用链接时使用路由名称。
如果您想了解更多信息,请查看文档here。
UPDATE 为了澄清您需要在对CreatedAtRoute的调用中使用路由名称。像这样:
return CreatedAtRoute("GetProductImage", new { id = Review.ImageId }, Review);
如果仍然无效,你可以发布你的路线配置吗?