Thursday, June 4, 2020

.Net Core 3.1 with oData code only

for notes with explanation.

Startup.cs

using System;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Builder;
using OdataDemo.Models;
using Microsoft.OData.Edm;

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



        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOData();
            services.AddControllers(mvcOptions =>
            mvcOptions.EnableEndpointRouting = false)
                .AddNewtonsoftJson();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            //app.UseEndpoints(endpoints =>
            //{
            //    endpoints.MapControllers();
            //});

            IEdmModel model = GetEdmModel(app.ApplicationServices);

            app.UseMvc(routeBuilder =>
            {
                routeBuilder.Select().Filter().Count().OrderBy().Expand().MaxTop(null);
                routeBuilder.MapODataServiceRoute("odata", "odata", model);
                routeBuilder.EnableDependencyInjection();
            });
        }
        private static IEdmModel GetEdmModel(IServiceProvider serviceProvider)
        {
            var builder = new ODataConventionModelBuilder(serviceProvider);
            builder.EntitySet<Student>("Students");
            return builder.GetEdmModel();
        }
    }
}

Controller/StudentsController.cs

using System;
using System.Collections.Generic;
using Microsoft.AspNet.OData;
using OdataDemo.Models;

namespace OdataDemo.Controllers
{
    public class StudentsController: ODataController
    {
        [EnableQuery()]
        public IEnumerable<Student> Get()
        {
            return new List<Student>
            {
                CreateNewStudent("Cody Allen", 130),
                CreateNewStudent("Viral Pandya", 140),
                CreateNewStudent("student3", 120),
                CreateNewStudent("student4", 150),
                CreateNewStudent("student5", 100)
            };
        }

        public Student CreateNewStudent(string name, int score)
        {
            return new Student
            {
                Id = Guid.NewGuid(),
                Name = name,
                Score = score
            };
        }
    }
}

Model/Students.cs
namespace OdataDemo.Models
{
    public class Student
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public int Score { get; set; }
    }
}

No comments:

Post a Comment