我不确定我错过了什么。这段代码在我的本地开发机器上运行良好,当我将它部署到测试服务器上时,它不断抛出内部服务器错误或“消息”:“请求包含实体主体但没有 Content-Type 标头。推断的媒体类型此资源不支持“应用程序/八位字节流”。
我在这里做的一切正确吗?我只是插入简单的学生数据。这里一个 webapi 函数插入从两个对象(Student 和 StudentAddress)收集的数据。所以我将这两个对象存储到模型中,然后在 WebAPI 中使用它们。
我仍然想知道它是如何在我的本地机器上工作的。我已将所有文件部署到测试服务器。
学生模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebAPI.Models
{
public class StudentModel
{
public Student Student { get; set; }
public StudentAddress StudentAddress { get; set; }
}
}
学生控制器
// POST: api/Student/InsertStudent
[ResponseType(typeof(StudentModel))]
[HttpPost]
public HttpResponseMessage InsertStudent(StudentModel studentModel)
{
objCSADBEntities.Student.Add(studentModel.Student);
objCSADBEntities.SaveChanges();
objCSADBEntities.StudentAddress.Add(studentModel.StudentAddress);
objCSADBEntities.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, studentModel);
}
在提交按钮上单击调用具有此代码的 Angularjs 提交函数
var Student = {
Name: $('#txt-FacilityLegalName').val().trim(),
LastDate: '2016-12-09 05:24:55.407',
SSMA_TimeStamp: '0x000000000778FCB1',
};
var StudentAddress = {
AddressLine1: $('#txt-MailingAddressLine1').val().trim(),
AddressLine2: ($('#txt-MailingAddressLine2').val().trim() != '' ? $('#txt-MailingAddressLine2').val().trim() : null),
City: $('#txt-City').val(),
State: $scope.objselectedstate.selectedstate.Value,
ZipCode: $('#txt-ZipCode').val().trim(),
ZipExt: ($('#txt-ZipExt').val().trim() != '' ? $('#txt-ZipExt').val().trim() : null),
FullName: $('#txt-ContactFirstName').val().trim() + ' ' + $('#txt-ContactLastName').val().trim(),
FirstName: $('#txt-ContactFirstName').val().trim(),
LastName: $('#txt-ContactLastName').val().trim(),
PhoneNumber: $('#txt-ContactPhone').val().trim(),
Ext: ($('#txt-ContactPhoneExt').val().trim() != '' ? $('#txt-ContactPhoneExt').val().trim() : null),
Fax: ($('#txt-ContactFax').val().trim() != '' ? $('#txt-ContactFax').val().trim() : null),
Email: $('#txt-Email').val().trim()
};
var studentModelModel = { studentAddress: StudentAddress, student: Student };
//Insert Student
var insertStudent = providerService.insertStudent($scope.webAPIHostName, studentModelModel);
insertStudent.then(function (response) {
//success response here
}, function (error) {
// error handling here
});
Angularjs Service.js
(function () {
var app = angular.module('myApp');
app.factory('studentService', ['$http', function ($http) {
var factory = [];
//Insert Student
factory.insertStudent = function (webAPIHostName, studentModel) {
var request = $http({
method: 'POST',
url: webAPIHostName + '/Provider/InsertStudent',
data: studentModel
})
return request;
}
return factory;
}
]);
})();
在控制台中,我看到了这个请求和响应
Request URL: http://localhost:5555/webapi/api/Student/InsertStudent
Request Method: POST
Status Code: 415 Unsupported Media Type
Referrer Policy: no-referrer-when-downgrade
Cache-Control: no-cache
Content-Length: 108
Content-Type: application/json; charset=utf-8
Date: Fri, 10 Jan 2020 23:50:25 GMT
Expires: -1
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-UA-Compatible: IE=Edge
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 374
Content-Type: application/octet-stream; charset=UTF-8
Cookie: ASP.NET_SessionId=qaqikpomv1fuqlorb5difdxp