我的Web应用程序向我的web api项目发送POST或DELETE HTTP请求。 POST工作正常,但DELETE抛出以下http 500错误:
"Message":"An error has occurred.",
"ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'EasNotification' from content with media type'text/plain'.",
"ExceptionType":"System.InvalidOperationException",
"StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)\r\n at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()\r\n at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)"
POST和DELETE在标题中都有以下内容:
接受:application / json,text / plain,
发布和删除的Hook对象都是相同的JSON对象。
我想了解为什么DELETE抛出异常?
这是我的控制器方法:
public void DeleteHook(Hook hook)
{
try
{
var hookRepo = new HookRepository(); <--- Breakpoint never reached
hookRepo.DeleteNotificationHook(hook);
}
catch (Exception ex)
{
var httpStatus = statusBuilder.GetStatusCode(ex.GetType());
var httpError = new HttpError(ex, true);
var errorResponse = Request.CreateErrorResponse(httpStatus, httpError);
throw new HttpResponseException(errorResponse);
}
}
public void PostHook(Hook hook)
{
try
{
var hookRepo = new HookRepository();
hookRepo.UpsertHook(hook);
}
catch (Exception ex)
{
var httpStatus = statusBuilder.GetStatusCode(ex.GetType());
var httpError = new HttpError(ex, true);
var errorResponse = Request.CreateErrorResponse(httpStatus, httpError);
throw new HttpResponseException(errorResponse);
}
}