简体   繁体   English

将 OData v4 注入 MVC 6

[英]Injecting OData v4 into MVC 6

I am currently hoping someone adventurous may have tackled this obstacle, as the current builds for MVC 6 running on ASP.Net v5.0 do not have any services I can find to load OData into the pipeline.我目前希望有冒险精神的人可能已经解决了这个障碍,因为在 ASP.Net v5.0 上运行的 MVC 6 的当前版本没有我可以找到的任何服务来将 OData 加载到管道中。 I invoke the app.UseMvc() and can construct convention routing, but cannot define any HttpConfiguration object in the new process.我调用了 app.UseMvc() 并且可以构造约定路由,但不能在新进程中定义任何 HttpConfiguration 对象。 I was really hoping to work with the combined MVC/WebApi in MVC 6, but OData v4 is a game changer.我真的希望在 MVC 6 中使用组合的 MVC/WebApi,但 OData v4 改变了游戏规则。

If anyone has had experience and could point me in the correct direction, please advise:如果有人有经验并且可以指出我正确的方向,请指教:

It may not help greatly, but here is my Startup class:它可能没有多大帮助,但这是我的 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();

        }
    }
}

ASP.NET MVC 6 does not yet support OData. ASP.NET MVC 6 尚不支持 OData。 To host OData in ASP.NET I would currently recommend using ASP.NET Web API 2.x, which supports both OData v3 and OData v4.要在 ASP.NET 中托管 OData,我目前建议使用 ASP.NET Web API 2.x,它支持 OData v3 和 OData v4。

If you want to use OData in an ASP.NET 5 app, you can use the OWIN bridge to host Web API 2.x on ASP.NET 5, but it still won't be using MVC 6.如果您想在 ASP.NET 5 应用程序中使用 OData,您可以使用OWIN 桥在 ASP.NET 5 上托管 Web API 2.x,但它仍然不会使用 MVC 6。

You'd then have some code like this (based on the aforementioned bridge):然后你会有一些这样的代码(基于前面提到的桥):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM