请求包含实体主体但没有Content-Type标头

时间:2016-11-21 19:28:04

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

我在尝试发布数据时遇到 415不支持的媒体类型错误。响应有 “此资源不支持推断媒体类型'application / octet-stream'。虽然内容类型设置为 application / json

有谁看到这里出了什么问题?

ApiController:

[HttpPost]
[Route( @"api/Assignments/{id:int}" )]
public HttpResponseMessage Save( int id, [FromBody] Assignment assignment )
{
    try
    {
        int result = 0;

              .
              .
              .

        return Request.CreateResponse( HttpStatusCode.OK );
    }
    catch( Exception ex )
    {
        //log error
        return Request.CreateResponse( HttpStatusCode.BadRequest );
    }
}

服务:

service.saveAssignment = function (assignment) {
    var serviceurl = baseURL + "api/Assignments/" + (assignment.ID || 0);
    return $http({
        method: "POST",
        data: angular.toJson(assignment),
        dataType: "json",
        headers: { "Content-Type": "application/json; charset=UTF-8;" },
        url: serviceurl
    });
};

请求:

POST http://localhost:31415/api/Assignments/0 HTTP/1.1
Host: localhost:31415
Connection: keep-alive
Content-Length: 43
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://localhost:31415
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Content-Type: application/json; charset=UTF-8;
DNT: 1
Referer: http://localhost:31415/Settings
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

{"SemesterID":3,"CourseID":1,"FacultyID":1}

响应:

{“Message”:“请求包含实体主体但没有Content-Type标头。  此资源不支持推断的媒体类型'application / octet-stream'。“,”ExceptionMessage“:”没有MediaTypeFormatter可用于从具有媒体类型的内容中读取“Assignment”类型的对象'application / octet-stream'。“,”ExceptionType“:”System.Net.Http.UnsupportedMediaTypeException“, “StackTrace”:“在System.Net.Http.Http.ModelBinding.FormatterParameterBinding的System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent内容,Type类型,IEnumerable'1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)\ r \ n .ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable'1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)“}

0 个答案:

没有答案