WebAPI and ODataController return 406 Not Acceptable

前端 未结 13 1251
余生分开走
余生分开走 2020-12-02 12:25

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

       config.Routes.MapHttpRoute(
            name: \"ApiById\",
            routeTempl         


        
13条回答
  •  自闭症患者
    2020-12-02 13:09

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

    The filter and routeTable parts weren't there when I started my project and are needed.

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

    And as a bonus, here's my 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 }
        );
    }
    

    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.

    So I can call:

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

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

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

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

提交回复
热议问题