Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I invoke the app.UseMvc() and can construct convention routing, but cannot define any HttpConfiguration object in the new process. I was really hoping to work with the combined MVC/WebApi in MVC 6, but OData v4 is a game changer.

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:

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

        }
    }
}
like image 616
Nathan Teague Avatar asked Dec 09 '14 19:12

Nathan Teague


People also ask

Are MVC and Web API into one in MVC 6?

ASP.NET MVC 6 is a Cross-Platform and Compatible Framework; the essential thing in this framework is it builds the frameworks into a single one by merging the frameworks of WebPages, MVC, and Web API. Moreover, it is rich in developing Web Apps and APIs using Model-View-Controller design patterns.

Why TempData is used in MVC?

What is TempData and How to Use in MVC? TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.

How do you pass data from view to controller in MVC using ViewData?

To pass the strongly-typed data from Controller to View using ViewData, we have to make a model class then populate its properties with some data and then pass that data to ViewData dictionary as Value and selecting Key's name is the programmer's choice.

Does OData require Entity Framework?

Enable Entity Framework For this tutorial, we'll use Entity Framework (EF) Code First to create the back-end database. Web API OData does not require EF. Use any data-access layer that can translate database entities into models.


1 Answers

ASP.NET MVC 6 does not yet support 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.

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.

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);
    };
}
like image 57
Eilon Avatar answered Oct 13 '22 05:10

Eilon