odata query works again ApiController but does not work again ODataController

1k views Asked by At

I try use System.Web.OData.ODataController in WebAPI 2.

WebConfig.cs

        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<User>("User");
        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: builder.GetEdmModel());

Controller:

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);
        }
    }
}

When I try query data I get error:

GET http://localhost:58786/api/lookups/users 406 (Not Acceptable)

When I replace ODataController with ApiController query works good.

Request Header:

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

ODataController Response header:

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 Response Header

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?=

On client side I use JayData.

What is wrong ? Any idea?

1

There are 1 answers

1
Sam Xu On BEST ANSWER

First, from the namespace System.Web.OData.ODataController, I think you are using the Web API OData V4 library. V4 doesn't accept application/atomsvc+xml because only "Json" is the standard in OData V4 spec.

Second, odata=fullmetadata is the OData V3 metadata header, for V4, it should be odata.metadata=full

Third, [Route("api/lookups/users")] is Web API attribute. If you want to use ODataController, please use the OData version. [ODataRoute("...")]

Fourth, please make sure the route template in [Route(...)] follows up the OData Uri conventions. See more detail here

Fifth, you can get more tutorial from here.

Hope it can help you. Thanks.