[英]ASP.NET WebAPI OData 406
I have an issue with getting a fairly simple OData WebAPI service to work. 我有一个问题,让一个相当简单的OData WebAPI服务工作。 Outside of the metadata query on ~/ and ~/$metadata checking my own entity set returns a 406. When debugging the controller (see below) the GET request gets routed through, the db is queried and data is actually returned.
在〜/和〜/ $元数据检查之外的元数据查询之外,我自己的实体集返回406.当调试控制器(见下文)时,GET请求被路由,查询数据库并实际返回数据。 However a 406 is still generated afterwards.
然而,之后仍然生成406。
The expected response is a properly formatted JSON response string showing all lines of data when querying ~/Crops. 预期的响应是格式正确的JSON响应字符串,显示查询〜/ Crops时的所有数据行。
Note that some of this is still black magic to me so feel free to elaborate on any responses. 请注意,其中一些对我来说仍然是黑魔法,所以请随时详细说明任何回复。 Here's the wiring:
这是接线:
Global.asax Global.asax中
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
WebApiConfig: WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.AddODataQueryFilter();
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Crop>("Crops");
var model = builder.GetEdmModel();
config.MapODataServiceRoute(
routeName: "odata",
routePrefix: null,
model: model);
}
}
Crop Model: 作物模型:
public class Crop
{
[Key]
public int CropId { get; set; }
[Required]
public string Name { get; set; }
public int MinCost { get; set; }
public int DefaultCost { get; set; }
public int MaxCost { get; set; }
}
CropsController (Excerpt): CropsController(摘录):
public class CropsController : ODataController
{
private ParadiseDataContext db = new ParadiseDataContext();
// GET: odata/Crops
[EnableQuery]
public IQueryable<Crop> GetCrops()
{
return db.Crops;
}
Queries URLs plus results: 查询网址和结果:
Home: 家:
http://localhost:39086/
{
"@odata.context":"http://localhost:39086/$metadata","value":[
{
"name":"Crops","kind":"EntitySet","url":"Crops"
}
]
}
Querying Crops 查询作物
http://localhost:39086/Crops
406 - Not Accepted (Empty Body)
Project file: https://www.dropbox.com/s/5k62xl8bdfbzgq7/ParadiseBayDataService.zip?dl=0 All of the binaries have been removed, all VSSO references have been removed. 项目文件: https ://www.dropbox.com/s/5k62xl8bdfbzgq7/ParadiseBayDataService.zip?dl =0所有二进制文件都已删除,所有VSSO引用都已删除。 Nuget package configuration should restore dependencies.
Nuget包配置应该还原依赖项。
I figured out what the issue was: 我弄清楚问题是什么:
Again the model (now full code) 再次模型(现在完整的代码)
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.OData;
using ParadiseBayDataService.Models;
namespace ParadiseBayDataService.Controllers
{
[EnableQuery]
public class CropsController : ODataController
{
private ParadiseDataContext db = new ParadiseDataContext();
// GET: odata/Crops
[EnableQuery]
public IQueryable<Crop> GetCrops()
{
return db.Crops;
}
The thing to note here is the line: 这里要注意的是这条线:
using System.Web.Http.OData;
When I changed this to: 当我把它改成:
using System.Web.OData;
It works as expected. 它按预期工作。 I did see a link that references the same thing.
我确实看到了一个引用相同内容的链接。 Just the answer to the question was more like "Use System.Web.OData" without much context.
只是问题的答案更像是“使用System.Web.OData”而没有太多的上下文。 I got to this by going back to the basics following a guide.
我通过回到导游后的基础知识来实现这一目标。 The one major difference between that working sample and what I had was this one line.
这个工作样本与我所拥有的一个主要区别就是这一行。 The routing doesn't give you much to go on through debugging, the crux seemingly being in the "EnableQuery"
路由并没有给你很多进行调试,这个症结果似乎在“EnableQuery”中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.