62,199
社区成员
发帖
与我相关
我的任务
分享
QueryStringMapping jsonMapping = new QueryStringMapping("type", "json", "application/json");
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(jsonMapping);
public MediaTypeFormatter FindReader(Type type, MediaTypeHeaderValue mediaType)
{
if (type == null)
{
throw Error.ArgumentNull("type");
}
if (mediaType == null)
{
throw Error.ArgumentNull("mediaType");
}
foreach (MediaTypeFormatter formatter in base.Items)
{
if ((formatter != null) && formatter.CanReadType(type))
{
foreach (MediaTypeHeaderValue value2 in formatter.SupportedMediaTypes)
{
if ((value2 != null) && value2.IsSubsetOf(mediaType))
{
return formatter;
}
}
continue;
}
}
return null;
}
去翻了下源代码,这应该是查找反序列化用的formatter的方法
可以通过设置
//var f = config.Formatters.JsonFormatter;
//f.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("mimetype"));
让JsonFormatter支持其他contentype..但是不能用空值..
MediaTypeMapping jsonMapping = new RequestHeaderMapping("custom", "test1", StringComparison.CurrentCultureIgnoreCase, true, "application/json");
config.Formatters.JsonFormatter.MediaTypeMappings.Add(jsonMapping);
MediaTypeMapping xmlMapping = new RequestHeaderMapping("custom", "test2", StringComparison.CurrentCultureIgnoreCase, true, "application/xml");
config.Formatters.XmlFormatter.MediaTypeMappings.Add(xmlMapping);
MediaTypeMapping jsonMapping = new RequestHeaderMapping("custom","test", StringComparison.CurrentCultureIgnoreCase,true,"application/json");
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(jsonMapping);
public class Answer
{
/// <summary>
/// 获取或设置 答案ID
/// </summary>
[Description("答案id")]
public int AID { get; set; }
/// <summary>
/// 获取或设置 问题ID
/// </summary>
[Description("问题id")]
public int QID { get; set; }
/// <summary>
/// 获取或设置 答案
/// </summary>
[Description("答案")]
public string AnswerContent { get; set; }
/// <summary>
/// 获取或设置 问题类型ID
/// </summary>
[Description("问题类型id")]
public int QTID { get; set; }
/// <summary>
/// 获取或设置 题组ID
/// </summary>
[Description("题组id")]
public int QGID { get; set; }
}