代码改变世界

关于 ASP.NET Core 中的 OData

2019-06-30 15:22  音乐让我说  阅读(885)  评论(0编辑  收藏  举报

1. BooksController

using BooksODataService.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Query;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace BooksODataService.Controllers
{
    public class BooksController : ODataController
    {
        private readonly BooksContext _booksContext;
        public BooksController(BooksContext booksContext)
        {
            _booksContext = booksContext;
        }

        [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
        public IQueryable<Book> Get(ODataQueryOptions options)
        {
            ODataValidationSettings settings = new ODataValidationSettings()
            {
                MaxExpansionDepth = 4
            };
            options.Validate(settings);
            var books = _booksContext.Books.Include(b => b.Chapters);
            return books;
        }

        [EnableQuery()]
        public SingleResult<Book> Get([FromODataUri] int key)
        {
            return SingleResult.Create(_booksContext.Books.Where(b => b.Id == key));
        }

    }

}

 

2. ChaptersController

using BooksODataService.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BooksODataService.Controllers
{
    public class ChaptersController : ODataController
    {
        private readonly BooksContext _booksContext;
        public ChaptersController(BooksContext booksContext)
        {
            _booksContext = booksContext;
        }

        public IQueryable<BookChapter> Get() =>
            _booksContext.Chapters.Include(c => c.Book);

        [EnableQuery]
        public SingleResult<BookChapter> Get([FromODataUri] int key) =>
            SingleResult.Create(_booksContext.Chapters.Where(c => c.Id == key));

    }
}

 

3. Startup.cs

using BooksODataService.Models;
using BooksODataService.Services;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

// OData Beta 1 docu http://odata.github.io/WebApi/#14-01-netcore-beta1

namespace BooksODataService
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddTransient<CreateBooksService>();
            services.AddDbContext<BooksContext>(options =>
            {
                string connString = Configuration.
                    GetConnectionString("BooksConnection");
                options.UseSqlServer(connString);
            });
            services.AddOData();
        }
        public void Configure(IApplicationBuilder app, 
            IHostingEnvironment env, CreateBooksService sampleBooks)
        {
            sampleBooks.CreateDatabase();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            var builder = new ODataConventionModelBuilder(app.ApplicationServices);
            builder.EntitySet<Book>("Books");
            builder.EntitySet<BookChapter>("Chapters");
            app.UseMvc(routeBuilder =>
            {
                routeBuilder.MapODataServiceRoute("ODataRoute", 
                    "odata", builder.GetEdmModel());
                routeBuilder.EnableDependencyInjection(); // workaround for Beta 1
            });
        }
    }
}

 

运行截图:

 

 

 

 

代码下载:https://files.cnblogs.com/files/Music/ODataSample-In-ASPNET-Core.rar