Response payload is not odata payload

24 views Asked by At

I am struggling to get an asp.net core web api with ODATA working properly. While it seems that the GET endpoint responds to the select, filter, order functions, the payload does not seem to be an odata payload. I am not sure what i am missing.

ie. instead of getting the expected payload of odata.

{
  "@odata.context": "...",
  "value": [...]
}

i am getting just the value i.e.:

[...]

FYI, i am using Microsoft.AspNetCore.OData version 8.2.5

This is my controller:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using PublicMarketsDataModel.Data;

namespace PublicMarketsDataModel.Controllers
{
    [Route("odata")]
    public class FungibleEquityController: ODataController
    {
        private readonly MasterDataDbContext _context;

        public FungibleEquityController(MasterDataDbContext context)
        {
            _context = context;
        }

        // GET: odata/FungibleEquities
        [HttpGet("FungibleEquity")]
        [EnableQuery]
        public IQueryable<FungibleEquity> Get()
        {
            return _context.FungibleEquities.AsQueryable();
        }
    }   
}

and this is the Program.cs

using Microsoft.AspNetCore.OData;
using Microsoft.EntityFrameworkCore;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using PublicMarketsDataModel.Data;

// Build a config object, using env vars and JSON providers.
IConfigurationRoot config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables()
    .Build();   

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
IEdmModel GetEdmModel()
{
    var builder = new ODataConventionModelBuilder();
    builder.EntitySet<FungibleEquity>("FungibleEquities");
    return builder.GetEdmModel();
}
builder.Services.AddControllers()
    .AddOData(opt => opt.AddRouteComponents("odata", GetEdmModel()).EnableQueryFeatures() );


// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<MasterDataDbContext>(options => options.UseSqlServer(config.GetConnectionString("DefaultConnection")));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

would anyone know why i am not getting the ODATA payload format and what's required to get it?

0

There are 0 answers