的WebAPI和ODataController返回406不可接受 [英] WebAPI and ODataController return 406 Not Acceptable

查看:340
本文介绍了的WebAPI和ODataController返回406不可接受的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加的OData到我的项目,我的路线,其中设置是这样的:

Before adding OData to my project, my routes where set up like this:

       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(动作名称创建),补丁(动作的名字是更新)和Delete。作为一个例子,客户端使用这些各种标准的URL为CustomerType请求:

All controllers provide Get, Put (action name is Create), Patch (action name is Update) and Delete. As an example, the client uses these various standard url's for the CustomerType requests:

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控制器。我还添加了新的路径:

Then I added an OData controller with the same action names as my other Api controllers. I also added a new route:

        ODataConfig odataConfig = new ODataConfig();

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

到目前为止,我改变了在客户端什么都没有。当我发送一个请求,我得到一个406不可用错误。

So far I changed nothing on the client side. When I send a request, I get a 406 Not Available error.

是路线混淆了?我怎样才能解决这个问题?

Are the routes getting mixed up? How can I solve this?

推荐答案

在该路由配置都有影响的顺序。就我而言,我也有一些标准的MVC控制器和帮助页面。因此,在的Global.asax

The order in which the routes are configured has an impact. In my case, I also have some standard MVC controllers and help pages. So in 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);
}

过滤器和routeTable部分不存在,当我开始我的项目,是的需要

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

这必须是在确切顺序即可。我试图四处移动电话,结束了要么MVC,API或使用OData的404或406错误打破了。

This has to be in that EXACT ORDER. I tried moving the calls around and ended up with either MVC, Api or Odata broken with 404 or 406 errors.

所以,我可以打电话:

本地主机:XXX / - >导致帮助页面(家居控制器,索引页)

localhost:xxx/ -> leads to help pages (home controller, index page)

本地主机:XXX / API / - >导致OData的$元

localhost:xxx/api/ -> leads to the OData $metadata

本地主机:XXX / API /站点 - >导致了我的SitesController从ODataController继承的Get方法

localhost:xxx/api/Sites -> leads to the Get method of my SitesController inheriting from ODataController

本地主机:XXX / API /测试 - >导致了我从的TestController继承ApiController Get方法

localhost:xxx/api/Test -> leads to the Get method of my TestController inheriting from ApiController.

这篇关于的WebAPI和ODataController返回406不可接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆