C# ODataWebAPI 2-Property方法返回xml而不是json
我使用的是ODataWebAPI 2。 所有操作(get entityset、get entity by id…)都返回json 当我尝试获取导航属性时,它返回预期的json响应。 但是当我试图获取属性时,响应是xml,这是不期望的 这是我的控制器代码:C# ODataWebAPI 2-Property方法返回xml而不是json,c#,xml,json,asp.net-web-api,odata,C#,Xml,Json,Asp.net Web Api,Odata,我使用的是ODataWebAPI 2。 所有操作(get entityset、get entity by id…)都返回json 当我尝试获取导航属性时,它返回预期的json响应。 但是当我试图获取属性时,响应是xml,这是不期望的 这是我的控制器代码: /// <summary> /// Gets the navigation property. /// </summary> /// <param name="key">The
/// <summary>
/// Gets the navigation property.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public virtual HttpResponseMessage GetNavigationProperty([FromODataUri] string key) {
ODataPath oDataPath = this.Request.GetODataPath();
var navigationPropertyName = (oDataPath.Segments[2] as NavigationPathSegment).NavigationPropertyName;
return GetProperty(key, navigationPropertyName);
}
/// <summary>
/// Gets the property.
/// </summary>
/// <returns></returns>
public virtual HttpResponseMessage GetProperty() {
ODataPath oDataPath = this.Request.GetODataPath();
var propertyName = (oDataPath.Segments[2] as PropertyAccessPathSegment).PropertyName;
var key = (oDataPath.Segments[1] as KeyValuePathSegment).Value;
return GetProperty(key, propertyName);
}
private HttpResponseMessage GetProperty(string key, string propertyName) {
var entity = GetEntities(key, new[] { propertyName }).FirstOrDefault();
var content = typeof(TEntity).GetProperty(propertyName).GetValue(entity, null);
// There are two System.Net.Http.HttpRequestMessageExtensions classes in two different dlls, we want the one on System.Web.Http.dll
var assembly = AppDomain.CurrentDomain.GetAssemblies().First(x => x.FullName.StartsWith("System.Web.Http,"));
var type = assembly.GetType("System.Net.Http.HttpRequestMessageExtensions");
// Method with parameter T, can't get it via GetMethod so we look it up as the only CreateResponse with 3 parameters.
MethodInfo method = type.GetMethods().First(x => x.Name == "CreateResponse" && x.GetParameters().Length == 3);
MethodInfo generic = method.MakeGenericMethod(content.GetType());
var response = generic.Invoke(Request, new[] { Request, HttpStatusCode.OK, content });
// Return our response.
return response as HttpResponseMessage;
}
//
///获取导航属性。
///
///钥匙。
///
公共虚拟HttpResponseMessage GetNavigationProperty([FromODataUri]字符串键){
ODataPath ODataPath=this.Request.GetODataPath();
var navigationPropertyName=(oDataPath.Segments[2]作为NavigationPathSegment);
返回GetProperty(键,navigationPropertyName);
}
///
///获取属性。
///
///
公共虚拟HttpResponseMessage GetProperty(){
ODataPath ODataPath=this.Request.GetODataPath();
var propertyName=(oDataPath.Segments[2]作为PropertyAccessPathSegment);
var key=(oDataPath.Segments[1]作为KeyValuePathSegment.Value;
返回GetProperty(键,propertyName);
}
私有HttpResponseMessage GetProperty(字符串键、字符串属性名称){
var entity=GetEntities(key,new[]{propertyName});
var content=typeof(tenty).GetProperty(propertyName).GetValue(entity,null);
//在两个不同的dll中有两个System.Net.Http.HttpRequestMessageExtensions类,我们希望在System.Web.Http.dll中有一个
var assembly=AppDomain.CurrentDomain.GetAssemblies().First(x=>x.FullName.StartsWith(“System.Web.Http”);
var type=assembly.GetType(“System.Net.Http.HttpRequestMessageExtensions”);
//带有参数T的方法,无法通过GetMethod获取它,因此我们将其查找为具有3个参数的唯一CreateResponse。
MethodInfo method=type.GetMethods().First(x=>x.Name==“CreateResponse”&&x.GetParameters().Length==3);
MethodInfo generic=method.MakeGenericMethod(content.GetType());
var response=generic.Invoke(请求,新[]{Request,HttpStatusCode.OK,content});
//回复我们的回复。
作为HttpResponseMessage返回响应;
}
我做错什么了吗?
谢谢
Omri.这是预期的,因为大多数浏览器的accept头中都有application/xml。例如,chrome发送这个消息
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
OData协议指定实体和实体集合的格式为application/atom+xml
,属性(复杂、基本、复杂集合、基本集合)的格式为application/xml
。在json方面,上述所有内容都可以并且将被格式化为application/json
当您从浏览器请求一个实体或提要时,上面的accept标头中没有任何内容类型可以根据OData规范格式化实体/提要。因此,我们默认为
application/json
。但是,如果您请求一个属性(非导航),则有一个匹配项来自application/xml
的Accept头。因此,我们返回application/xml
我从未尝试过使用OData的Web API 2,但据我所知,响应格式不是由客户端发送的Accept
头控制的吗?您是否为该标题指定了application/json
?据我所知,您完全正确。问题是,我没有定义任何具体的东西。刚刚用我的浏览器测试了这个服务。使用fiddler并发送正确的accept头解决了我的问题。