我创建了一个虚拟项目来测试VS2015中的Odata,我遇到了与此问题中所述完全相同的问题,而且我的代码在很大程度上等同于那里描述的内容。 Web API 2: OData 4: Actions returning 404OData查询给出了404错误,除非我添加了一个斜杠
任何对绑定函数的查询都会给出404错误,直到您添加尾部斜线。例如:
http://localhost:46092/odata/v1/Trips/Default.GetTripNameById - 404 http://localhost:46092/odata/v1/Trips/Default.GetTripNameById/ - 按预期工作
http://localhost:46092/odata/v1/Trips/Default.GetTripNameById(tripID=1) $选择=名称 - 404 http://localhost:46092/odata/v1/Trips/Default.GetTripNameById(tripID=1)/ $选择=名称 - 按预期工作
这是不应该发生的,因为?微软文档永远不会提到需要结尾的斜杠,他们的例子应该没有它们。此外,这打破了不添加尾部斜杠的Swagger UI,并在尝试进行任何查询时获取404。
这种行为的原因是什么?我如何使它不用斜线工作,这似乎是正常的预期行为?
这里是我的代码片段:
TripsController.cs:
...
[HttpGet]
public IHttpActionResult GetTripNameById(int tripID)
{
return Ok(DemoDataSources.Instance.Trips.AsQueryable().Where(t => t.ID == tripID.ToString()));
}
WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.MapODataServiceRoute("odata", "odata/v1", GetEdmModel());
DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.EnsureInitialized();
}
private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>("People");
builder.EntitySet<Trip>("Trips");
builder.EntityType<Trip>().Collection.Function("GetTripNameById").Returns<string>().Parameter<int>("tripID");
var edmModel = builder.GetEdmModel();
return edmModel;
}
我还试图从这里所有的web.config配置选项:http://stackoverflow.com/questions/11728846/dots- in-url-causes-404-with-asp-net-mvc-and-iis - 并没有任何效果。我在VS2015项目中将它们添加到web.debug.config中。 – K48
web.debug.config是错误的地方(正如你在下面的答案中注意的那样)。正如你现在可能知道的那样,它只在部署项目时使用,并且发布设置设置为部署解决方案的“调试”配置。对于本地主机开发,只能使用您在主web.config中看到的内容 – bkwdesign