Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api odata查询在ApicController中再次工作,但在ODataController中无法再次工作_Asp.net Web Api_Odata_Jaydata_Asp.net Apicontroller_Odatacontroller - Fatal编程技术网

Asp.net web api odata查询在ApicController中再次工作,但在ODataController中无法再次工作

Asp.net web api odata查询在ApicController中再次工作,但在ODataController中无法再次工作,asp.net-web-api,odata,jaydata,asp.net-apicontroller,odatacontroller,Asp.net Web Api,Odata,Jaydata,Asp.net Apicontroller,Odatacontroller,我尝试在WebAPI 2中使用System.Web.OData.ODataController WebConfig.cs ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<User>("User"); config.MapODataServiceRoute( routeName: "ODataRoute

我尝试在WebAPI 2中使用System.Web.OData.ODataController

WebConfig.cs

        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<User>("User");
        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: builder.GetEdmModel());
尝试查询数据时,出现错误:

GET http://localhost:58786/api/lookups/users 406 (Not Acceptable)
当我用ApiController替换ODataController时,查询效果很好

请求头:

public class UserApiODataController : ODataController
{
    [Route("api/lookups/users")]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
    public IHttpActionResult GetUsers()
    {
        try
        {
            var context = new DbContenxt();
            return Ok(context.Users.AsQueryable());
        }
        catch (Exception exception)
        {
            return InternalServerError(exception);
        }
    }
}
Accept:application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
DataServiceVersion:2.0
Host:localhost:58786
MaxDataServiceVersion:2.0
Origin:http://localhost:62131
Pragma:no-cache
Referer:htpp://localhost:62131/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
Remote Address:[::1]:58786
Request URL:http://localhost:58786/api/lookups/users
Request Method:GET
Status Code:406 Not Acceptable
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:62131
Cache-Control:no-cache
Content-Length:0
Date:Mon, 30 Nov 2015 16:09:25 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpccHJvamVjdHNcZG9rdW1lbnRhXFN3YWxsb3dcU3JjXFN3YWxsb3cuV2ViQXBpXGFwaVxsb29rdXBzXExpY2Vuc2VUeXBl?=
ODataController响应标题:

public class UserApiODataController : ODataController
{
    [Route("api/lookups/users")]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
    public IHttpActionResult GetUsers()
    {
        try
        {
            var context = new DbContenxt();
            return Ok(context.Users.AsQueryable());
        }
        catch (Exception exception)
        {
            return InternalServerError(exception);
        }
    }
}
Accept:application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
DataServiceVersion:2.0
Host:localhost:58786
MaxDataServiceVersion:2.0
Origin:http://localhost:62131
Pragma:no-cache
Referer:htpp://localhost:62131/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
Remote Address:[::1]:58786
Request URL:http://localhost:58786/api/lookups/users
Request Method:GET
Status Code:406 Not Acceptable
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:62131
Cache-Control:no-cache
Content-Length:0
Date:Mon, 30 Nov 2015 16:09:25 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpccHJvamVjdHNcZG9rdW1lbnRhXFN3YWxsb3dcU3JjXFN3YWxsb3cuV2ViQXBpXGFwaVxsb29rdXBzXExpY2Vuc2VUeXBl?=
ApiController响应标题

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:62131
Cache-Control:no-cache
Content-Length:247
Content-Type:application/json; charset=utf-8
Date:Mon, 30 Nov 2015 16:20:53 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpccHJvamVjdHNcZG9rdW1lbnRhXFN3YWxsb3dcU3JjXFN3YWxsb3cuV2ViQXBpXGFwaVxsb29rdXBzXExpY2Vuc2VUeXBl?=
在客户端,我使用JayData


怎么了?有什么想法吗

首先,从名称空间System.Web.OData.ODataController中,我认为您正在使用Web API OData V4库。V4不接受应用程序/atomsvc+xml,因为ODataV4规范中只有“Json”是标准

其次,odata=fullmetadata是odatav3元数据头,对于V4,它应该是odata.metadata=full

第三,
[Route(“api/lookups/users”)]
是Web api属性。如果要使用ODataController,请使用OData版本<代码>[ODataRoute(“…”)

第四,请确保[route(…)]中的路由模板遵循ODataURI约定。查看更多详细信息

第五,您可以从中获得更多教程


希望它能帮助你。谢谢。

问题出在请求头中,而且ODataRoute也不正确,谢谢