[英]Error occurred when returning XML - ASP.net web API
我需要获取基于请求URL的XML和JSON响应数据。要达到我的要求,我将此代码行添加到了global.asax
API的Application_Start
方法中
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
此代码可以很好地获取JSON。 我这样叫API
对于JSON输出: http://localhost:2751/api/student?type=JSON
对于XML输出: http://localhost:2751/api/student?type=xml
但是当我要求XML时会发生此错误。
“ ObjectContent`1”类型未能序列化内容类型“ application / xml”的响应主体; charset = utf-8'。
这是内部异常消息。
不应使用数据协定名称为“ StudentModel: http : //schemas.datacontract.org/2004/07/DeleteAPI.Models ”的类型为“ DeleteAPI.Models.StudentModel”。 如果您正在使用DataContractSerializer或将任何不是静态已知的类型添加到已知类型的列表中,请考虑使用DataContractResolver-例如,通过使用KnownTypeAttribute属性或将它们添加到传递给序列化程序的已知类型的列表中。
这是我的模型课程“学生”的一部分。
public class StudentModel
{
public int StudentID { get; set; }
public int ProjectID { get; set; }
public string WorkDate { get; set; }
public Nullable<int> WorkingHours { get; set; }
public Nullable<int> Overtime { get; set; }
public string Descriptions { get; set; }
}
属于控制器类,
namespace DeleteAPI.Controllers
{
public class StudentController : ApiController
{
// GET: api/Student
public ArrayList Get()
{
StudentPersistent tp = new StudentPersistent();
return tp.getStudentAll();
}
}
}
这是StudentPersistent.class
的一部分
public class StudentPersistent
{
public ArrayList getStudentAll()
{
ArrayList stdArray = new ArrayList();
MySql.Data.MySqlClient.MySqlDataReader mySQLReader = null;
try
{
string sqlString = "select * from tblStudent";
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlString, conn);
mySQLReader = cmd.ExecuteReader();
while (mySQLReader.Read())
{
StudentModel dm = new StudentModel();
dm.StudentID = mySQLReader.GetInt32(0);
dm.ProjectID = mySQLReader.GetInt32(1);
dm.WorkDate = mySQLReader.GetString(2);
dm.WorkingHours = mySQLReader.GetInt32(3);
dm.Overtime = mySQLReader.GetInt32(4);
dm.Descriptions = mySQLReader.GetString(5);
stdArray.Add(dm);
}
}
catch (MySqlException x)
{
Console.WriteLine(x.Number);
}
return stdArray;
}
}
我该如何解决该错误,原因是什么?
尝试将操作方法更改为:
[HttpGet]
public HttpResponseMessage Get(int isxmlorJson)
{
StudentPersistent tp = new StudentPersistent();
tp = //Get data from Business Layer
var data = new ObjectContent<StudentPersistent>(tp,
((isxmlorJson== 1) ? Configuration.Formatters.XmlFormatter :
Configuration.Formatters.JsonFormatter));
return new HttpResponseMessage()
{
Content = data
};
}
这是定义控制器的一般方法,可以删除application_start中的配置。
问题解决了。 我在模型类中添加了KnownType
属性。
[KnownType(typeof(DeleteAPI.Models.TaskModel))]
public class StudentModel
{
public int DeveloperID { get; set; }
public int ProjectID { get; set; }
public string WorkDate { get; set; }
public Nullable<int> WorkingHours { get; set; }
public Nullable<int> Overtime { get; set; }
public string Descriptions { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.