Web API和OData控制器返回406 Not Acceptable

时间:2014-10-31 14:20:31

标签: c# asp.net-web-api odata

在将OData添加到我的项目之前,我的路由设置如下:

       config.Routes.MapHttpRoute(
            name: "ApiById",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"^[0-9]+$" },
            handler: sessionHandler
        );

        config.Routes.MapHttpRoute(
            name: "ApiByAction",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { action = "Get" },
            constraints: null,
            handler: sessionHandler
        );

        config.Routes.MapHttpRoute(
            name: "ApiByIdAction",
            routeTemplate: "api/{controller}/{id}/{action}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"^[0-9]+$" },
            handler: sessionHandler

所有控制器都提供Get,Put(操作名称为Create),Patch(操作名称为Update)和Delete。例如,客户端将这些各种标准URL用于CustomerType请求:

string getUrl =  "api/CustomerType/{0}";
string findUrl = "api/CustomerType/Find?param={0}";
string createUrl = "api/CustomerType/Create";
string updateUrl = "api/CustomerType/Update";
string deleteUrl = "api/CustomerType/{0}/Delete";

然后我添加了一个OData控制器,其动作名称与我的其他Api控制器相同。我还添加了一条新路线:

        ODataConfig odataConfig = new ODataConfig();

        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: odataConfig.GetEdmModel()
        );

到目前为止,我在客户端没有改变任何内容。当我发送请求时,我收到406 Not Available错误。

路线混乱了吗?我该如何解决这个问题?

13 个答案:

答案 0 :(得分:73)

如果您使用的是OData V4,请替换using System.Web.Http.OData;

使用using System.Web.OData;请查看最新资料库的评论

ODataController中的

对我有用。

答案 1 :(得分:17)

配置路由的顺序会产生影响。就我而言,我还有一些标准的MVC控制器和帮助页面。所以在Global.asax

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(config =>
    {
        ODataConfig.Register(config); //this has to be before WebApi
        WebApiConfig.Register(config); 

    });
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

当我启动项目并且需要时,过滤器和路由表部件不在那里。

ODataConfig.cs

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes(); //This has to be called before the following OData mapping, so also before WebApi mapping

    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

    builder.EntitySet<Site>("Sites");
    //Moar!

    config.MapODataServiceRoute("ODataRoute", "api", builder.GetEdmModel());
}

WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute( //MapHTTPRoute for controllers inheriting ApiController
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
    );
}

作为奖励,这是RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute( //MapRoute for controllers inheriting from standard Controller
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

这必须是 EXACT ORDER 。我尝试移动调用,最终导致MVC,Api或Odata因404或406错误而崩溃。

所以我可以致电:

localhost:xxx / - &gt;导致帮助页面(家庭控制器,索引页面)

localhost:xxx / api / - &gt;导致OData $元数据

localhost:xxx / api / Sites - &gt;导致我的SitesController的Get方法继承自ODataController

localhost:xxx / api / Test - &gt;导致我的TestController的Get方法继承自ApiController。

答案 2 :(得分:11)

将routePrefix设置为“api”。

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<CustomerType>("CustomerType");

config.MapODataServiceRoute(routeName: "ODataRoute", routePrefix: "api", model: builder.GetEdmModel());

您使用的是哪个OData版本?对于V3 System.Web.OData,检查OData V4使用System.Web.Http.OData的名称空间是否正确。控制器中使用的命名空间必须与WebApiConfig中使用的命名空间一致。

答案 3 :(得分:6)

另一件需要考虑的事情是URL区分大小写,所以:

localhost:xxx/api/Sites -> OK
localhost:xxx/api/sites -> HTTP 406

答案 4 :(得分:6)

我的问题与返回实体模型而不是我公开的模型(builder.EntitySet<ProductModel>("Products");)有关。解决方案是将实体映射到资源模型。

答案 5 :(得分:4)

此页面上的所有优秀解决方案都不适合我。通过调试,我可以看到路由被拾取并且OData查询正确运行。然而,在控制器退出后,它们变得严重受损,这表明正在生成看似OData全部错误的格式:406 Not Acceptable。

我通过添加基于Json.NET库的自定义格式化程序来修复此问题:

public class JsonDotNetFormatter : MediaTypeFormatter
{
    public JsonDotNetFormatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
    }

    public override bool CanReadType(Type type)
    {
        return true;
    }

    public override bool CanWriteType(Type type)
    {
        return true;
    }

    public override async Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
    {
        using (var reader = new StreamReader(readStream))
        {
            return JsonConvert.DeserializeObject(await reader.ReadToEndAsync(), type);
        }
    }

    public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {
        if (value == null) return;
        using (var writer = new StreamWriter(writeStream))
        {
            await writer.WriteAsync(JsonConvert.SerializeObject(value, new JsonSerializerSettings {ReferenceLoopHandling = ReferenceLoopHandling.Ignore}));
        }
    }

然后在WebApiConfig.cs中,我添加了行config.Formatters.Insert(0, new JsonDotNetFormatter())。请注意,我正密切关注杰特答案中描述的顺序。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ConfigureODataRoutes(config);
        ConfigureWebApiRoutes(config);
    }

    private static void ConfigureWebApiRoutes(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
    }

    private static void ConfigureODataRoutes(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Formatters.Insert(0, new JsonDotNetFormatter());
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<...>("<myendpoint>");
        ...
        config.MapODataServiceRoute("ODataRoute", "odata", builder.GetEdmModel());
    }
}

答案 6 :(得分:3)

我遇到的问题是我将我的实体集命名为“Products”并拥有一个ProductController。结果实体集的名称必须与您的控制器名称匹配。

所以

builder.EntitySet<Product>("Products");

使用名为ProductController的控制器会出错。

/ api / Product将给出406

/ api / Products将给出404

因此,使用一些新的C#6功能,我们可以这样做:

builder.EntitySet<Product>(nameof(ProductsController).Replace("Controller", string.Empty));

答案 7 :(得分:3)

The problem/solution in my case was even more stupid. I'd left test code in my action that returned a completely different model type, just a Dictionary, and not my proper EDM model type.

Though I protest that the use of HTTP 406 Not Acceptable to communicate the error of my ways, is equally as stupid.

答案 8 :(得分:1)

在GitHub中发现错误:“无法使用odata $ select,$ expand,其他人默认使用#511”,他们的解决方案是在注册路径之前输入以下行:

// enable query options for all properties
config.Filter().Expand().Select().OrderBy().MaxTop(null).Count();

对我来说就像一个魅力。

来源:https://github.com/OData/RESTier/issues/511

答案 9 :(得分:1)

我的错误和修复与上面的答案不同。

我遇到的具体问题是在WebApi 2.2中的ODataController中访问mediaReadLink端点。

OData在规范中有一个“默认流”属性,允许返回的实体拥有附件。所以例如filter等的json对象描述了该对象,然后嵌入了一个也可以访问的媒体链接。就我而言,它是所描述对象的PDF版本。

这里有一些卷曲问题,第一个来自配置:

<system.web>
  <customErrors mode="Off" />
  <compilation debug="true" targetFramework="4.7.1" />
  <httpRuntime targetFramework="4.5" />
  <!-- etc -->
</system.web>

起初我试图返回FileStreamResult,但我相信这不是默认的net45运行时。因此管道不能将其格式化为响应,并且不能接受406。

此处的修复是返回HttpResponseMessage并手动构建内容:

    [System.Web.Http.HttpGet]
    [System.Web.Http.Route("myobjdownload")]
    public HttpResponseMessage DownloadMyObj(string id)
    {
        try
        {
            var myObj = GetMyObj(id); // however you do this                
            if (null != myObj )
            {
                HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.OK);

                byte[] bytes = GetMyObjBytes(id); // however you do this
                result.Content = new StreamContent(bytes); 

                result.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/pdf");
                result.Content.Headers.LastModified = DateTimeOffset.Now;  
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(DispositionTypeNames.Attachment)
                {
                    FileName = string.Format("{0}.pdf", id),
                    Size = bytes.length,
                    CreationDate = DateTimeOffset.Now,
                    ModificationDate = DateTimeOffset.Now
                };

                 return  result;
            }
        }
        catch (Exception e)
        {
            // log, throw 
        }
        return null;
    }

我的最后一个问题是在返回有效结果后收到意外的500错误。添加常规异常过滤器后,我发现错误为Queries can not be applied to a response content of type 'System.Net.Http.StreamContent'. The response content must be an ObjectContent.。这里的修复是从控制器声明的顶部删除[EnableQuery]属性,并仅在返回实体对象的端点的操作级别应用它。

[System.Web.Http.Route("myobjdownload")]属性是如何使用web api 2.2在OData V4中嵌入和使用媒体链接。为了完整起见,我将完全放弃下面的完整设置。

首先,在我的Startup.cs

[assembly: OwinStartup(typeof(MyAPI.Startup))]
namespace MyAPI
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // DI etc
            // ...
            GlobalConfiguration.Configure(ODataConfig.Register); // 1st
            GlobalConfiguration.Configure(WebApiConfig.Register); // 2nd      
            // ... filters, routes, bundles etc
            GlobalConfiguration.Configuration.EnsureInitialized();
        }
    }
}

ODataConfig.cs

// your ns above
public static class ODataConfig
{
    public static void Register(HttpConfiguration config)
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        var entity1 = builder.EntitySet<MyObj>("myobj");
        entity1.EntityType.HasKey(x => x.Id);
        // etc

        var model = builder.GetEdmModel();

        // tell odata that this entity object has a stream attached
        var entityType1 = model.FindDeclaredType(typeof(MyObj).FullName);
        model.SetHasDefaultStream(entityType1 as IEdmEntityType, hasStream: true);
        // etc

        config.Formatters.InsertRange(
                                    0, 
                                    ODataMediaTypeFormatters.Create(
                                                                    new MySerializerProvider(),
                                                                    new DefaultODataDeserializerProvider()
                                                                    )
                                    );

        config.Select().Expand().Filter().OrderBy().MaxTop(null).Count();

        // note: this calls config.MapHttpAttributeRoutes internally
        config.Routes.MapODataServiceRoute("ODataRoute", "data", model);

        // in my case, i want a json-only api - ymmv
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeWithQualityHeaderValue("text/html"));
        config.Formatters.Remove(config.Formatters.XmlFormatter);

    }
}

WebApiConfig.cs

// your ns above
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // https://stackoverflow.com/questions/41697934/catch-all-exception-in-asp-net-mvc-web-api
        //config.Filters.Add(new ExceptionFilter());

        // ymmv
        var cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);

        // so web api controllers still work
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // this is the stream endpoint route for odata
        config.Routes.MapHttpRoute("myobjdownload", "data/myobj/{id}/content", new { controller = "MyObj", action = "DownloadMyObj" }, null);
        // etc MyObj2
    }
}

MySerializerProvider.cs

public class MySerializerProvider: DefaultODataSerializerProvider
{
    private readonly Dictionary<string, ODataEdmTypeSerializer> _EntitySerializers;

    public SerializerProvider()
    {
        _EntitySerializers = new Dictionary<string, ODataEdmTypeSerializer>();
        _EntitySerializers[typeof(MyObj).FullName] = new MyObjEntitySerializer(this);
        //etc 
    }

    public override ODataEdmTypeSerializer GetEdmTypeSerializer(IEdmTypeReference edmType)
    {
        if (edmType.IsEntity())
        {
            string stripped_type = StripEdmTypeString(edmType.ToString());
            if (_EntitySerializers.ContainsKey(stripped_type))
            {
                return _EntitySerializers[stripped_type];
            }
        }            
        return base.GetEdmTypeSerializer(edmType);
    }

    private static string StripEdmTypeString(string t)
    {
        string result = t;
        try
        {
            result = t.Substring(t.IndexOf('[') + 1).Split(' ')[0];
        }
        catch (Exception e)
        {
            //
        }
        return result;
    }
}

MyObjEntitySerializer.cs

public class MyObjEntitySerializer : DefaultStreamAwareEntityTypeSerializer<MyObj>
{
    public MyObjEntitySerializer(ODataSerializerProvider serializerProvider) : base(serializerProvider)
    {
    }

    public override Uri BuildLinkForStreamProperty(MyObj entity, EntityInstanceContext context)
    {
        var url = new UrlHelper(context.Request);
        string id = string.Format("?id={0}", entity.Id);
        var routeParams = new { id }; // add other params here
        return new Uri(url.Link("myobjdownload", routeParams), UriKind.Absolute);            
    }

    public override string ContentType
    {
        get { return "application/pdf"; }            
    }
}

DefaultStreamAwareEntityTypeSerializer.cs

public abstract class DefaultStreamAwareEntityTypeSerializer<T> : ODataEntityTypeSerializer where T : class
{
    protected DefaultStreamAwareEntityTypeSerializer(ODataSerializerProvider serializerProvider)
        : base(serializerProvider)
    {
    }

    public override ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext)
    {
        var entry = base.CreateEntry(selectExpandNode, entityInstanceContext);

        var instance = entityInstanceContext.EntityInstance as T;

        if (instance != null)
        {
            entry.MediaResource = new ODataStreamReferenceValue
            {
                ContentType = ContentType,
                ReadLink = BuildLinkForStreamProperty(instance, entityInstanceContext)
            };
        }
        return entry;
    }

    public virtual string ContentType
    {
        get { return "application/octet-stream"; }
    }

    public abstract Uri BuildLinkForStreamProperty(T entity, EntityInstanceContext entityInstanceContext);
}

最终结果是我的json对象嵌入了这些odata属性:

odata.mediaContentType=application/pdf
odata.mediaReadLink=http://myhost/data/myobj/%3fid%3dmyid/content

以下解码的媒体链接http://myhost/data/myobj/?id=myid/content会在MyObjController : ODataController上触发端点。

答案 10 :(得分:0)

在我的情况下,我需要将非公共财产制定者更改为公开。

public string PersonHairColorText { get; internal set; }

需要改为:

public string PersonHairColorText { get; set; }

答案 11 :(得分:0)

在我的情况下(odata V3)我不得不将OdataController的名称改为与提供的相同 ODataConventionModelBuilder并解决了这个问题

我的控制员:

public class RolesController : ODataController
{
    private AngularCRMDBEntities db = new AngularCRMDBEntities();

    [Queryable]
    public IQueryable<tROLE> GetRoles()
    {
        return db.tROLEs;
    }
}

ODataConfig.cs:

public class ODataConfig
{
    public static void Register(HttpConfiguration config)
    {
        ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();
        modelBuilder.EntitySet<WMRole>("RolesNormal"); 
        modelBuilder.EntitySet<WMCommon.DAL.EF.tROLE>("Roles").EntityType.HasKey(o => o.IDRole).HasMany(t => t.tROLE_AUTHORIZATION);
        modelBuilder.EntitySet<WMCommon.DAL.EF.tLOOKUP>("Lookups").EntityType.HasKey(o => o.IDLookup).HasMany(t => t.tROLE_AUTHORIZATION);
        modelBuilder.EntitySet<WMCommon.DAL.EF.tROLE_AUTHORIZATION>("RoleAuthorizations").EntityType.HasKey(o => o.IDRoleAuthorization);

        config.Routes.MapODataRoute("odata", "odata", modelBuilder.GetEdmModel());
        config.EnableQuerySupport();
    }
}

WebApiConfig.cs:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));            

        config.Routes.MapHttpRoute( //MapHTTPRoute for controllers inheriting ApiController
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
            );

        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
            .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters
            .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
    }
}

的Global.asax:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(config =>
        {
            ODataConfig.Register(config); 
            WebApiConfig.Register(config);
        });            
    }
}

答案 12 :(得分:0)

对我来说,问题是,我使用LINQ并直接选择了加载的对象。 我必须使用select new才能工作:

return Ok(from u in db.Users
          where u.UserId == key
          select new User
          {
              UserId = u.UserId,
              Name = u.Name
          });

没有起作用:

return Ok(from u in db.Users
          where u.UserId == key
          select u);