将OData v4注入MVC​​ 6

Nat*_*gue 6 c# asp.net-core-mvc odata-v4

我目前希望冒险的人可能已经解决了这个障碍,因为在ASP.Net v5.0上运行的MVC 6的当前版本没有任何我能找到的将OData加载到管道中的服务.我调用app.UseMvc()并可以构造约定路由,但不能在新进程中定义任何HttpConfiguration对象.我真的希望能够在MVC 6中使用MVC/WebApi,但OData v4是一个改变游戏规则的游戏.

如果有人有经验并且可以指出我正确的方向,请告知:

它可能没什么用,但这是我的Startup类:

using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Data.OData;
// Won't work, but needs using System.Web.OData.Builder;
using Microsoft.Framework.DependencyInjection;

namespace bmiAPI
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {

            app.UseWelcomePage();
            app.UseMvc();

        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Eil*_*lon 3

ASP.NET MVC 6 尚不支持 OData。要在 ASP.NET 中托管 OData,我目前建议使用 ASP.NET Web API 2.x,它支持 OData v3 和 OData v4。

如果要在 ASP.NET 5 应用程序中使用 OData,可以使用OWIN 桥在 ASP.NET 5 上托管 Web API 2.x,但它仍然不会使用 MVC 6。

然后你会得到一些像这样的代码(基于前面提到的桥):

public void Configure(IApplicationBuilder app)
{
    // Use OWIN bridge to map between ASP.NET 5 and Katana / OWIN
    app.UseAppBuilder(appBuilder =>
    {
        // Some components will have dependencies that you need to populate in the IAppBuilder.Properties.
        // Here's one example that maps the data protection infrastructure.
        appBuilder.SetDataProtectionProvider(app);


        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration(); 
        config.Routes.MapHttpRoute( 
            name: "DefaultApi", 
            routeTemplate: "api/{controller}/{id}", 
            defaults: new { id = RouteParameter.Optional } 
        );

        appBuilder.UseWebApi(config);
    };
}
Run Code Online (Sandbox Code Playgroud)