Injecting OData v4 into MVC 6



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

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

它可能没有太大帮助,但这是我的启动类:

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

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

最新更新