使用.Net Core v1.1创建OData服务,它可以很好地从浏览器连接,但从Excel 2016连接时失败,作为基本的OData Feed.例外是
‘InvalidOperationException’
No media types found in ‘Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter.SupportedMediaTypes’.
Add at least one media type to the list of supported media types.
Excel和浏览器都使用Method =’GET’连接到’http://localhost:52315/odata‘
来自浏览器的HeaderAccept是:
"text/html, application/xhtml+xml, image/jxr, */*"
来自excel的Header接受:(注意:为了便于阅读,我在’;’之后添加了新行)
"application/json;
odata.metadata=minimal;
q=1.0,application/json;
odata=minimalmetadata;
q=0.9,application/atomsvc+xml;
q=0.8,application/atom+xml;
q=0.8,application/xml;
q=0.7,text/plain;
q=0.7"
有关如何添加媒体类型以及应添加哪些媒体类型的任何见解都表示赞赏.似乎无法找到样品或以前的问题处理此事.
最佳答案 这显然是一个已知问题(参见
https://github.com/OData/WebApi/issues/597).它似乎不太可能在vNext分支中解决,但幸运的是,我找到了一个解决方法.
在Startup.cs中,在注册Odata之后,添加以下代码段:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOData();
services.AddMvcCore(options =>
{
// loop on each OData formatter to find the one without a supported media type
foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
{
// to comply with the media type specifications, I'm using the prs prefix, for personal usage
outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.dummy-odata"));
}
});
}
这将找到所有未声明支持的媒体类型的输出格式化程序,并添加虚拟媒体类型.
参考文献:
> vNext code that adds an improper formatter
> same code in the master branch