WebAPI和ODataController返回406不可接受。

23 浏览
0 Comments

WebAPI和ODataController返回406不可接受。

在将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";

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

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

到目前为止,我在客户端方面什么也没改变。当我发送请求时,我收到一个406 Not Available错误。

路由是否混淆了?我该如何解决这个问题?

0