ODATA网页API 404错误 [英] Odata web api 404 error
问题描述
我是新来的OData。我所建的与控制器的Web API,如下所示:
I am new to odata. I have builded a web api with a controller as shown below:
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Web.OData.Routing;
namespace HelloWebApi.Controllers
{
public class TestsController : ODataController
{
//
// GET: /Product/
ProductsContext db = new ProductsContext();
private bool TestExists(int key)
{
return db.tests.Any(p => p.key== key);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
[EnableQuery]
public IQueryable<test> Get()
{
return db.tests;
}
的模型是如下所示:
The model is as shown below:
public class Test
{
[Key]
public int key { get; set; }
public string aaa { get; set; }
}
我也构成如下routeconfig,odataconfig,配置的WebAPI和Global.asax文件。
I have also configured routeconfig, odataconfig, webapi config and global.asax file as shown below.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "Default",
routeTemplate: "{controller}/{action}/{id}"
);
}
}
public class ODataConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Test>("Tests");
config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
下面显示的是在Global.asax文件:
The below shown is the global.asax file:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(config =>
{
ODataConfig.Register(config); //this has to be before WebApi
WebApiConfig.Register(config);
});
//FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
我试图使大量的修改。但我正在逐渐HTTP 404未找到错误。我尝试添加[ODataRoute]属性的操作方法名。当这样做,我得到的Http 406不能接受的回应。
I tried making a lot of modification. But I am getting Http 404 not found error. I tried adding [ODataRoute] attribute to the action method name. When doing that I am getting Http 406 not acceptable response.
我想的URL为:的http://本地主机:6701 / ODATA /测试/ 其中,ODATA是后缀和试验是控制器的名字。请什么我做错了建议。
The url I am trying is : http://localhost:6701/odata/tests/ where odata is the suffix and tests is the controller name. Please suggest on what I am doing wrong.
推荐答案
在<一个配置的路由href=\"https://msdn.microsoft.com/en-us/library/system.web.http.odata.builder.odataconventionmodelbuilder%28v=vs.118%29.aspx\"相对=nofollow> ODataConventionModelBuilder
是区分大小写的。在您的code,你定义了:
The routes configured by the ODataConventionModelBuilder
are case-sensitive. In your code, you have defined:
builder.EntitySet<Test>("Tests");
根据此,端点将的http://本地主机:6701 / ODATA /测试/ 一>(注意在测试的大写的T)。
Based on this, the endpoint will be http://localhost:6701/odata/Tests/ (note the upper-case "T" in "Tests").
这是由设计,以保持与OData的规范兼容。
This is by design, in order to maintain compatibility with the OData specification.
不过,正如的Web API的OData 5.4 的,你可以使用 HttpConfiguration
类的<code> EnableCaseInsensitive()方法也可以启用不区分大小写的路线。例如,在你的 ODataConfig.Register()
方法,你可以添加:
That said, as of Web API OData 5.4, you can optionally enable case-insensitive routes using the HttpConfiguration
class's EnableCaseInsensitive()
method. E.g., in your ODataConfig.Register()
method you could add:
config.EnableCaseInsensitive(caseInsensitive: true);
有关详细信息,请参阅基本不区分大小写的支持根据微软的的ASP.NET Web API的OData为V4文档。
For more information, see Basic Case Insensitive Support under Microsoft's ASP.NET Web API for OData V4 Docs.
这篇关于ODATA网页API 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!